1

首先,我不是程序员——所以我试图让事情适应我的需要。我已经在一个 html 表单上工作了几个星期,它产生一个唯一的代码并将数据通过电子邮件发送给我和表单所有者。

我已经设法让所有这些工作并将其构建到我的网站中。

然而,最后的痕迹是我想将电子邮件格式化为 html,并且我已经搜索并阅读并尝试了许多不同的代码 - 有些甚至从这里没有成功。我在一个不间断的句子中得到代码,什么都没有或所有信息。

这是我的 php.ini 文件。靠近底部的是我在邮件中需要的信息$email_message

表格代码:

<div style="position:absolute;left:0px;top:546px;width:350px;height:84px;">
<style type="text/css">
.formtxt{color:white;}
</style>
<?php
$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$serial = '';

for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 4; $j++) {
    $serial .= $tokens[rand(0, 35)];
}

if ($i < 2) {
    $serial .= '-';
}
}
?>
<form name="contactform" method="post" action="bridalcontact.php">
<table width="350px" align="center">
<tr>
<td valign="top">
<label for="first_name"> <span class="formtxt">First Name*</span></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name"> <span class="formtxt">Last Name*</span></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"> <span class="formtxt">Email*</span></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="security"> <span class="formtxt">What colour is the sky *</span></label>
</td>
<td valign="middle">
<input type="text" name="security" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Code</label>
</td>
<td valign="top">
<input type="disabled" name="code" size="20" value='<?php echo "$serial"?>'readonly>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>

php代码:

<?php

if(isset($_POST['email'])) {
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "my email address here";
  $email_subject = "Your AODJ voucher";
  function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted.    ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
  }
  // validation expected data exists
  if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['security'])) {
      died('We are sorry, but there appears to be a problem with the form you submitted.');
    }
  $first_name = $_POST['first_name']; // required
  $last_name = $_POST['last_name']; // required
  $email_from = $_POST['email']; // required
  $security = $_POST['security']; // required
  $code = $_POST['code']; // not required
  $error_message = "";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
  $security_exp = "/blue/";
  if(!preg_match($security_exp,$security)) {
    $error_message .= 'Wrong solar system - sorry.<br />';
  }
  //$string_exp = "/^[A-Za-z .'-]+$/";
  //if(!preg_match($security_exp,$security)) {
  //$error_message .= 'Wrong solar system - sorry.<br />';
  //}
  $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
  $email_message = "Form details below.\n\n";
  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }
  $email_message .= "First Name: ".clean_string($first_name)."\n";
  $email_message .= "Last Name: ".clean_string($last_name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "code: ".clean_string($code)."\n";
  // create email headers
  $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  @mail($email_from, $email_subject, $email_message, $headers);
?>
<?php
  if( !empty( $_POST ) ) {
    header( "Location: bridaloptionsthanks.html" ) ; exit ;
  }
}
?>
4

3 回答 3

1

提供完整的代码以将您的电子邮件作为 html 发送可能是一项漫长的工作,具体取决于您想要什么。

  1. 在您的代码中,我不得不提到您可能存在安全问题,因为您允许服务器从未经验证的表单发送电子邮件。在某种程度上,有人可能会使用您的服务器通过脚本自动发送垃圾邮件。

  2. 电子邮件可以根据 mime 格式以 html 格式发送,如果您想支持多部分内容(一个纯文本版本和一个 html 版本),实现起来可能会非常复杂。幸运的是,今天大多数客户都支持 html。

  3. 如果你想发送一个设计精美的html内容等等,你必须学习html和css。你应该注意邮件客户端,并不总是支持所有的 css 属性,这使得开发有点长。

于 2013-03-06T03:34:19.690 回答
1

经过测试,对我来说效果很好。试试这个。

[形式]

<?php
$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$serial = '';

for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 4; $j++) {
    $serial .= $tokens[rand(0, 35)];
}

if ($i < 2) {
    $serial .= "-";
}
}
?>

<!DOCTYPE html>

<head>

<style type="text/css">
.formtxt{color:black;}
</style>

</head>

<body>


<h1>My first heading</h1>

<p align="left">My first paragraph</p>

<div style="position:absolute;left:0px;top:140px;width:350px;height:84px;">
<form name="contactform" method="post" action="bridalcontact.php">
<table width="350px" align="center">
<tr>
<td valign="top">
<label for="first_name"><span class="formtxt">First Name*</span></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name"><span class="formtxt">Last Name*</span></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><span class="formtxt">Email*</span></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">

<label for="security"> <span class="formtxt">What colour is the sky *</span></label>

</td>
<td valign="middle">
<input type="text" name="security" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Code</label>
</td>
<td valign="top">
<input type="disabled" name="code" size="20" value="<?php echo "$serial"?>"readonly>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>

</body>
</html>

[加工]

<?php

if(isset($_POST['email'])) {
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "YOUR EMAIL HERE";
  $email_subject = "Your AODJ voucher";
  function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted.    ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
  }
  // validation expected data exists
  if(!isset($_POST['first_name']) || !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['security'])) {
      died('We are sorry, but there appears to be a problem with the form you submitted.');
    }
  $first_name = $_POST['first_name']; // required
  $last_name = $_POST['last_name']; // required
  $email_from = $_POST['email']; // required
  $security = $_POST['security']; // required
  $code = $_POST['code']; // not required
  $error_message = "";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }


  $security_exp = "/blue/";


  if(!preg_match($security_exp,$security)) {

    $error_message .= 'Wrong solar system - sorry.<br />';
  }

  $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
  $email_message = "Form details below.\n\n";
  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }
  $email_message .= "First Name: ".clean_string($first_name)."\n";
  $email_message .= "Last Name: ".clean_string($last_name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "code: ".clean_string($code)."\n";

  // create email headers

$headers = "Content-type: text/html\r\n";
$headers .= 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  @mail($email_from, $email_subject, $email_message, $headers);
?>
<?php
  if( !empty( $_POST ) ) {
    header( "Location: thank_you.php" ) ; exit ;
  }
}
?>

[谢谢]

<?php

echo "Thank you";

?>
于 2013-03-07T04:11:41.407 回答
1

为了发送 HTML 格式的邮件,您需要添加正确的标题信息。

更改此行:

$headers = 'From: '.$email_from."\r\n".
...

为此:(并添加一个点/连接$headers .=

$headers = "Content-type: text/html\r\n";
$headers .= 'From: '.$email_from."\r\n".
...

旁注:只有第一个标题行没有点/连接。它后面的其他标题将需要它。查阅 PHP.net 上的手册

然后在您希望的位置添加您的 HTML。

根据手册:

于 2013-03-06T03:28:36.243 回答