2012-11-10 109 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