PHP电子邮件脚本与自定义邮件正文

时间:2012-11-10 02:50:04

标签: php forms email html-email sendmessage

我正在使用PHP电子邮件脚本来填写联系表单。表格有六个字段:

  • 名称
  • 电子邮件
  • 电话号码
  • 预订日期
  • 预订时间
  • 评论

还有一个隐藏的honeypot字段用于robotest。 PHP脚本如下:

 <?php 

$robotest = $_POST['robotest']; //just testin' for robots

$recipient = "info@mydomain.com"; //recipient 
$email = ($_POST['email']); //senders e-mail adress 

if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) { 

$Name = ($_POST['name']); //senders name 

$mail_body = !!!----> WHAT DO I PUT HERE <----!!!! 

$subject = "Porter Inquiry"; //subject 
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields 

mail($recipient, $subject, $mail_body, $header); //mail command :) 

} else {
  print "You've entered an invalid email address!";
}
?>

你会注意到上面我放!!!----> WHAT DO I PUT HERE <----!!!的地方,我不确定如何将多个字段放入邮件正文中。我想包括类似的内容:

"Hello,

You have received a new booking with the following details:

Booking Time: ($_POST['time']) Booking Date:  ($_POST['date'])



Additional customer comments: ($_POST['comments']);

Please respond to the customer within 30 minutes on the following
phone number: ($_POST['phone'])

Warm regards,

Robot."

我找不到关于如何成功实现这一点的任何信息,非常感谢一些指导。

4 个答案:

答案 0 :(得分:3)

我稍微修改了Zoltan的代码。现在应该工作。

<?php 

$robotest = $_POST['robotest']; //just testin' for robots

$recipient = "info@mydomain.com"; //recipient 
$email = ($_POST['email']); //senders e-mail adress 

if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) { 

$Name = ($_POST['name']); //senders name 

$mail_body  = "Hello, \r\n";
$mail_body .= "You have received a new booking with the following details: \r\n";
$mail_body .= "Booking Time: ({$_POST['time']}) Booking Date: ({$_POST['date']}) \r\n";
$mail_body .= "Additional customer comments: ({$_POST['comments']}); \r\n";
$mail_body .= "Please respond to the customer within 30 minutes on the following phone number: ({$_POST['phone']}) \r\n";
$mail_body .= "Warm regards, \r\n";
$mail_body .= "Robot. \r\n";



$subject = "Porter Inquiry"; //subject 
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields 

mail($recipient, $subject, $mail_body, $header); //mail command :) 

} else {
  print "You've entered an invalid email address!";
 }
?>

希望这有帮助......是的,即使字段为空(收件人字段除外),这也会发送邮件

DINS

答案 1 :(得分:2)

你几乎完全按照你在问题中引用的内容。您可以将其编写为非常长的字符串或使用连接运算符:

$mail_body  = "Hello, \r\n";
$mail_body .= "You have received a new booking with the following details: \r\n";
$mail_body .= "Booking Time: (" . $_POST['time'] .") Booking Date: (". $_POST['date'] .") \r\n";
$mail_body .= "Additional customer comments: (". $_POST['comments'] ."); \r\n";
$mail_body .= "Please respond to the customer within 30 minutes on the following phone number: (". $_POST['phone'] .") \r\n";
$mail_body .= "Warm regards, \r\n";
$mail_body .= "Robot. \r\n";

答案 2 :(得分:2)

尝试发送HTML邮件

像这样修改您的邮件正文(当然,您可以对其进行更多更改)

$mailBody = "Hello,<br/><br/>";
$mailBody .= "You have received a new booking with the following details:<br/><br/>";
$mailBody .= "Booking Time: ".$_POST['time']." Booking Date:  ".$_POST['date']." <br/><br/><br/>";
$mailBody .= "Additional customer comments: ".$_POST['comments']."<br/><br/>";
$mailBody .= "Please respond to the customer within 30 minutes on the following<br/>";
$mailBody .= "phone number: ".$_POST['phone']."<br/><br/>";
$mailBody .= "Warm regards,<br/><br/>";
$mailBody .= "Robot.";

$header喜欢这个

$header = "From: ". $Name . " <" . $email . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

答案 3 :(得分:0)

披露:我是AlphaMail背后的开发者之一

如果您想轻松处理此问题,我建议您使用以下交易电子邮件服务:

<强>为什么吗

  • 您不必过多考虑电子邮件投放。
  • 统计。让我们跟踪Total Sent / Clicks / Opens / Bounces。
  • 通常是基于Web服务而不是SMTP。即更容易处理。
  • 清洁代码(至少如果您使用AlphaMail将数据与演示文稿分开)。
  • 可扩展且面向未来。

如果您选择使用AlphaMail,则可以使用AlphaMail PHP-client

示例:

include_once("comfirm.alphamail.client/emailservice.class.php");

$email_service = AlphaMailEmailService::create()
    ->setServiceUrl("http://api.amail.io/v1")
    ->setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");

$data = new stdClass();

$data->booking = new stdClass();
$data->booking->time = $_POST['time'];
$data->booking->date = $_POST['date'];
$data->booking->comment = $_POST['comments'];
$data->booking->phone = $_POST['phone'];

$response = $email_service->queue(EmailMessagePayload::create()
    ->setProjectId(12345) // Your AlphaMail project (determines template, options, etc)
    ->setSender(new EmailContact("Sender Company Name", "from@example.com"))
    ->setReceiver(new EmailContact("Joe Doe", "to@example.org"))
    ->setBodyObject($data) // Any serializable object
);

AlphaMail的另一个优点是您可以直接在AlphaMail Dashboard中修改模板,并且可以使用the Comlang template language格式化电子邮件。这样就可以创建类似下面的模板(文本版本示例,但您可以轻松创建HTML版本)。

Hello,

You have received a new booking with the following details:

Booking Time: <# payload.booking.time #>
Booking Date: <# payload.booking.date #>

Additional customer comments: <# payload.booking.comment #>
Please respond to the customer within 30 minutes on the following phone number: <# payload.booking.phone #>

Warm regards,

Robot