0

我正在尝试通过 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";
?>

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

4

3 回答 3

1

Are you executing this script on your local machine, or on a webserver with SMTP availability? If it's on your own computer, you may not have a mail server it can use.

Consider using PHPMailer instead, it is far more capable than the native mail() function, and includes integrated SMTP support: https://github.com/PHPMailer/PHPMailer

于 2013-08-25T09:03:20.557 回答
1

It is not a good idea to suppress error messages using @, you need to see these errors.

If you are testing this locally you need a mail server to be running. I use Test Mail Server Tool, it's dead simple.

Also note that mail() returning true does not indicate that the mail was successfully sent, only that execution of the command didn't generate an error:

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

于 2013-08-25T09:06:00.993 回答
0

这是我的工作代码:

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

也许标题中正确的回车很重要

于 2013-08-25T09:10:00.113 回答