发送电子邮件但未收到

时间:2013-08-25 08:59:36

标签: php email

我正在尝试通过php发送电子邮件

以下是代码:

<?php
//define the receiver of the email
$to = 'xxx@example.com';
//define the subject of the email
$subject = 'Test email'; 
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail."; 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: xxx@example.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

但是我的电子邮件中未收到该消息。

3 个答案:

答案 0 :(得分:1)

您是在本地计算机上还是在具有SMTP可用性的Web服务器上执行此脚本?如果它在您自己的计算机上,您可能没有可以使用的邮件服务器。

考虑使用PHPMailer,它比原生mail()功能更强大,并且包括集成的SMTP支持:https://github.com/PHPMailer/PHPMailer

答案 1 :(得分:1)

使用@来抑制错误消息不是一个好主意,您需要查看这些错误。

如果您在本地测试,则需要运行邮件服务器。我使用Test Mail Server Tool,这很简单。

另请注意,mail()返回true并不表示邮件已成功发送,只是执行该命令未产生错误:

  

如果邮件成功接受传递,则返回TRUE,FALSE   否则。

     

重要的是要注意,因为邮件被接受了   交付,并不意味着邮件实际上会达到预期的目的   目的地。

答案 2 :(得分:0)

这是我的工作代码:

mail($address, $subject, $message, "From: Service <service@service.net>\n".
                                   "MIME-Version: 1.0\n".
                                   "Content-type: text/plain; charset=\"UTF-8\"\n");

标题中的正确回车可能很重要

相关问题