通过 php mail() 函数发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21961821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send mail by php mail() function
提问by Beacze
I have problem with sending mail message by php mail()function. I'm not sure if it's problem with code coz I have read that some hosting servers are not allowing to sends mail but I'm trying to send this mail also when website is on localhost and it still doesn't work - after click "Send" I see the information: "Your mail is sent", but when I'm checking on my postbox there is no mails (also in spam).
我在通过 php mail()函数发送邮件消息时遇到问题。我不确定代码是否有问题,因为我读过一些托管服务器不允许发送邮件,但是当网站位于本地主机上时,我也尝试发送此邮件,但它仍然无法正常工作 - 单击后“发送”我看到信息:“您的邮件已发送”,但是当我查看邮箱时没有邮件(也在垃圾邮件中)。
For me code looks good but maybe I'm missing something. The second option which I'm considering is that also my localhost is not allowing to send mails.
对我来说,代码看起来不错,但也许我遗漏了一些东西。我正在考虑的第二个选项是我的本地主机也不允许发送邮件。
<form id="contact" action="mail.php" method="POST">
<div class="field">
<label class="fixed_width" for="name">Name:</label><input id="name" name="name" value="Name"/>
</div>
<div class="field">
<label class="fixed_width" for="surname">Surname:</label><input id="surname" name="surname" value="Surname"/>
</div>
<div class="field">
<label class="fixed_width" for="mail">E-mail:</label><input id="mail" name="mail" value="E-mail"/>
</div>
<div class="field" id="message">
<label class="fixed_width" id="message_width" for="mail">Message:</label>
<textarea id="message" name="message" />Type your message...</textarea>
</div>
<div>
<input class="width" type="submit" value="Send" />
</div>
</form>
<?php
srand((double)microtime()*1000000);
$marker = md5(uniqid(rand()));
$receiver = "[email protected]";
$title = "Mail";
$sender = $_POST['name'];
$sender .= $_POST['surname'];
$sender_mail = $_POST['mail'];
$message = $_POST['message'];
$headers = "From: $sender <$sender_mail>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= "\tboundary=\"___$marker==\"";
$content ="--___$marker==\n";
$content .="Content-Type: text/plain; charset=\"iso-8859-2\"\n";
$content .="Content-Transfer-Encoding: 8bit\n";
$content .="\n$message\n";
if (mail($receiver,$title,$content,$headers))
{
print "Your message is sent.";
} else {
print "Your message is not sent.
<br>Please go <a href=\"javascript:history.back();\">back</a> and send again.";
}
?>
Pictures with my php conf:
图片与我的 php conf:






回答by Floris
To test that sending of email works, try this really short program:
要测试发送电子邮件是否有效,请尝试这个非常简短的程序:
<?php
$email_to="[email protected]";
$email_subject="It works";
$email_message="Hello. I can send mail!";
$headers = "From: Beacze\r\n".
"Reply-To: [email protected]\r\n'" .
"X-Mailer: PHP/" . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
echo "mail sent!"
?>
I have used one "just like it" in the past as the starting point for testing a configuration. If this doesn't work it's most likely your server configuration.
过去,我曾使用“就像它一样”作为测试配置的起点。如果这不起作用,很可能是您的服务器配置。
回答by Abdallah Alhabarneh
You can use SMTP (phpmailer)
Example:
您可以使用 SMTP (phpmailer)
示例:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
You can find more about PHPMailer here: https://code.google.com/a/apache-extras.org/p/phpmailer/
您可以在此处找到有关 PHPMailer 的更多信息:https: //code.google.com/a/apache-extras.org/p/phpmailer/

