如何格式化输出从php表单到html电子邮件


How to format output from php form to html email

首先,我不是一个程序员-所以我试图适应我需要的东西。我已经工作了几个星期的html表单,产生一个独特的代码和电子邮件数据给我和表单所有者。

我已经设法得到这一切的工作,并建立到我的网站。

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

这是我的php。底部是我需要在$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 ;
  }
}
?>

提供一个完整的代码来发送你的电子邮件作为html可能是一个漫长的工作,这取决于你想要什么。

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

  2. 电子邮件可以发送在html根据mime格式,这可能是相当复杂的实现,如果你想支持多部分内容(一个版本在纯文本和一个版本在html)。幸运的是,现在大多数客户端支持html。

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

经过测试,对我来说很好。

(形式)

<?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";
?>

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

改变这一行:

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

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

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

旁注:只有第一个标题行没有点/连接。它后面的其他头文件将需要它。参考PHP.net手册

然后添加你想要的HTML。

按照手册:

  • http://php.net/manual/en/function.mail.php