Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有自定义邮件正文的PHP电子邮件脚本_Php_Forms_Email_Html Email_Sendmessage - Fatal编程技术网

带有自定义邮件正文的PHP电子邮件脚本

带有自定义邮件正文的PHP电子邮件脚本,php,forms,email,html-email,sendmessage,Php,Forms,Email,Html Email,Sendmessage,我正在使用一个PHP电子邮件脚本作为联系人表单。表单有六个字段: 名字 电子邮件 电话号码 预订日期 预定时间 评论 还有一个隐藏的蜜罐领域为机器人测试。PHP脚本如下所示: <?php $robotest = $_POST['robotest']; //just testin' for robots $recipient = "info@mydomain.com"; //recipient $email = ($_POST['email']); //senders e-ma

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

  • 名字
  • 电子邮件
  • 电话号码
  • 预订日期
  • 预定时间
  • 评论
还有一个隐藏的蜜罐领域为机器人测试。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!";
}
?>

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

您在这里几乎完全按照您在问题中引用的内容填写。您可以将其写入很长的字符串或使用串联运算符:

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

我稍微修改了佐尔坦的代码。现在应该可以工作了

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

尝试发送HTML邮件

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


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

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

  • 山楂
  • 发送网格
为什么?

  • 你不必考虑太多的电子邮件发送
  • 统计数字。让我们来跟踪发送/点击/打开/反弹的总数
  • 通常基于web服务而不是SMTP。也就是说,更容易处理
  • 更干净的代码(至少如果您使用分隔数据和表示的字母邮件)
  • 可扩展且经得起未来考验
如果你选择使用字母邮件,你可以使用

示例:

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的另一个优点是,您可以在中直接编辑模板,并且可以使用格式化电子邮件。这将使创建如下模板成为可能(文本版本示例,但您可以轻松创建HTML版本)

你好,
您已收到包含以下详细信息的新预订:
预订时间:
预订日期:
其他客户意见:
请在30分钟内通过以下电话号码回复客户:
亲切问候,,
机器人

如果没有填写所有字段,脚本还会发送电子邮件吗?不得不取消接收-结果表明邮件没有与您的解决方案一起发送(如果我删除了所有邮件,但
$mail\u body=“Hello,\r\n”
它发送得很好,但不会提交其他行。)我已更新了答案。双引号中的变量存在一些转义问题。即使没有填写任何字段,电子邮件仍将被发送-它只会错过该信息。但对表单进行某种验证是一种很好的做法,不要让他们这样做。或者在你身边抓住它,并在电子邮件发送脚本中处理它。
$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 = "From: ". $Name . " <" . $email . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
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
);
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