2013-03-06 97 views
1

首先,我不是程序员 - 所以我试图根据需要调整事情。我在一个html表单上工作了几个星期,这个html表单产生一个唯一的代码并将数据通过电子邮件发送给我和表单所有者。如何将输出格式从PHP表单格式化为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 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 

$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._%-][email protected][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 ; 
    } 
} 
?> 
+3

学习HTML。应用知识。 – ElefantPhace 2013-03-06 03:21:19

+4

我们都必须从某个地方开始。首先,摆脱@标志。他们正在吞咽您可能需要看到的错误。第二,如果你愿意,你可以打一次电话给BCC。我会将所有的\ n改成\ r \ n'。在
标签后面加上'\ r \ n',然后把
中的'/'加上去。你想让HTML看起来像什么?只是换行符?这是我第一次使用php发送HTML邮件的结果:http://css-tricks.com/sending-nice-html-email-with-php/ – mkaatman 2013-03-06 03:23:27

+0

你永远不会收到任何东西发送到HTML输出与您的编辑直到你添加'$ headers =“Content-type:text/html \ r \ n”;' – 2013-03-06 04:09:20

回答

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。

按手册:

+0

我已经在有些场合尝试过使用这些标头但没有成功。 我对“安全性”中的反垃圾邮件验证需要特定的答案。我认为这是令人满意的? – user2138201 2013-03-06 04:56:35

+0

其余的我将不得不尝试,当我在家里,可以再次工作的代码。 我想如果我可以得到非常基本的html来通过 - 我可以通过播放代码来格式化它。但我似乎没有能够从起跑线下车:-( – user2138201 2013-03-06 04:58:07

1

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

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

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

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

+0

这里是对css的参考:http://www.campaignmonitor.com/css/ – Bertrand 2013-03-06 03:35:25

+0

这里是发送多部分内容的简单方法: http://www.codingforums.com/showthread.php?t=142893 – Bertrand 2013-03-06 03:37:19

+0

是的,谢谢elefantphace我新的人会回来, – user2138201 2013-03-06 04:54:43

2

经过测试,适合我。试试这个。

【组成】

<?php 
$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 

$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._%-][email protected][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"; 

?> 
+0

嗨弗雷德 我知道代码的工作原理是,但我似乎无法成功注入html。我可以问....你会建议我把这个: < !DOCTYPE html>

我的第一个标题

我的第一段。

user2138201 2013-03-07 08:04:26

+0

@ user2138201我编辑了FORM部分,并为了测试目的而改变了一些东西。剩下的,如果它与HTML相关,则必须在网上学习教程。 – 2013-03-07 14:31:12

+0

@ user2138201如果此功能适用于您,请将我的答案标记为已接受。 – 2013-03-08 14:37:32