从脚本发送电子邮件的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";
}
?>
答
而不是使用原生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 = 'From: [email protected]' . "\r\n"
将显示谁送你的电子邮件找到快捷邮件的详细信息。它已经在你的脚本中。您不需要额外添加任何东西
+0
那个电子邮件目前是我们的。我需要它是填写表格的人。那么我如何从表单中添加他们的电子邮件地址? – JakeSays 2012-07-26 00:09:36
是否有错误? – 2012-07-25 23:03:10