从脚本发送电子邮件的PHP脚本

问题描述:

有人可以告诉我如何解决此脚本,以便表单信息发送到正确的电子邮件(即,我的电子邮件....让我们假设我的电子邮件是[email protected]) 。我希望电子邮件能够显示发送给我的用户的电子邮件地址。从脚本发送电子邮件的PHP脚本

<?php 
$name = $_POST['fldName']; 
$email = $_POST['fldEmail']; 
$phone = $_POST['fldPhone']; 
$comments = $_POST['fldComments']; 
$isFormValid = false; 

if (strlen($name) && strlen($email) && strlen($phone) && strlen($comments)) $isFormValid = true; 

if ($isFormValid) 

{ 


$to  = '[email protected]'; 
$subject = 'Contact Us Form Comments'; 

$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Comments: '.$comments; 

$headers = 'From: [email protected]' . "\r\n" 
     . 'Reply-To: [email protected]' . "\r\n"; 

mail($to, $subject, $message, $headers); 

header("location: thankyou.html"); 

} 
else 
{ 

echo "Please fill in the required fields"; 

} 

?> 
+0

是否有错误? – 2012-07-25 23:03:10

而不是使用原生PHP邮件()来自己完成所有的事情,最好使用一些像swift邮件程序这样的有用库,这使得您可以更轻松地发送邮件而不必担心内部工作。

例如发送邮件在迅捷邮件中,所有你需要做的是。

$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) 
    ->setUsername('your username') 
    ->setPassword('your password'); 
$mailer = Swift_Mailer::newInstance($transport); 
// Create a message 
$message = Swift_Message::newInstance('Wonderful Subject') 
    ->setFrom(array('[email protected]' => 'John Doe')) 
    ->setTo(array('[email protected]', '[email protected]' => 'A name')) 
    ->setBody('Here is the message itself'); 
// Send the message 
$result = $mailer->send($message); 

,你可以在那里官方网站http://swiftmailer.org/

更改了$headers变量使用发件人的电子邮件地址,而不是你自己的。

+0

我不知道用户的电子邮件地址,因为他们填写表单时的实际时间。它不应该是一个有价值的东西吗? – JakeSays 2012-07-26 00:08:30

+0

'$ email',有没有机会? – 2012-07-26 00:17:19

+0

也许??????但我把它放在哪里?在我发布在我的问题中的代码中,我如何确定我收到的电子邮件中的人员的电子邮件地址是正确的(非MINE),所以当我回复电子邮件时,电子邮件的地址在TO字段中,而不是我的 – JakeSays 2012-07-27 16:59:31

$headers = 'From: [email protected]' . "\r\n"将显示谁送你的电子邮件找到快捷邮件的详细信息。它已经在你的脚本中。您不需要额外添加任何东西

+0

那个电子邮件目前是我们的。我需要它是填写表格的人。那么我如何从表单中添加他们的电子邮件地址? – JakeSays 2012-07-26 00:09:36