2

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

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

还有一个隐藏的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

4 回答 4

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

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

丁斯

于 2012-11-10T04:49:29.390 回答
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";
于 2012-11-10T02:56:02.073 回答
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";
于 2012-11-10T05:07:52.157 回答
0

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

如果您想非常轻松地处理此问题,我建议您使用事务性电子邮件服务,例如:

为什么?

  • 您不必过多考虑电子邮件传递。
  • 统计数据。让您跟踪总发送/点击/打开/退回。
  • 通常基于 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中编辑您的模板,并且您可以使用Comlang 模板语言来格式化您的电子邮件。这样就可以创建如下模板(文本版本示例,但您可以轻松创建 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
于 2012-11-11T21:21:50.860 回答