2012-11-10 103 views
2

我正在为联系表单使用PHP电子邮件脚本。有六个字段的形式:带自定义邮件正文的PHP电子邮件脚本

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

还有一个隐藏的蜜罐场为robotest。 PHP脚本如下:

<?php 

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

$recipient = "[email protected]"; //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." 

我找不到任何信息如何成功实现这一点,将不胜感激一些指导。

回答

3

我已经修改了Zoltan的代码。现在应该工作。

<?php 

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

$recipient = "[email protected]"; //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

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"; 
+0

如果不是所有字段都填写完毕,脚本还会发送电子邮件吗? – Jascination

+0

必须不接受 - 证明邮件没有与您的解决方案一起发送(如果我删除了所有邮件,除了'$ mail_body =“Hello,\ r \ n”;'发送正常,但其他邮件不会发送提交。) – Jascination

+0

我已经更新了答案。它与双引号内的变量有一些转义问题。即使没有填充字段,电子邮件仍会被发送 - 它只会错过该信息。但对你的表单进行某种验证是一种很好的做法,不要让它们这样做。或者抓住你的身边,并通过电子邮件发送脚本来处理它。 –

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"; 
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", "[email protected]")) 
    ->setReceiver(new EmailContact("Joe Doe", "[email protected]")) 
    ->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