php mail() - 显示已发送但未在收件箱/垃圾邮件中收到的邮件

时间:2017-11-21 04:06:27

标签: php email

我有这个简单的程序,它在发送邮件时显示“成功”。但它实际上并未在收件箱或垃圾邮件中收到。可能是什么问题?

mail.php

<?php

$to = "xyz@gmail.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: abc@gmail.com";

$result = mail($to,$subject,$txt,$headers);

if(!$result) {
     echo "Error";
} else {
    echo "Success";
}

?>

1 个答案:

答案 0 :(得分:0)

如我的评论中所述,无法向/向Gmail地址发送未经授权的电子邮件。看看PHPMailer =&gt; https://github.com/PHPMailer/PHPMailer 下载zip文件https://github.com/PHPMailer/PHPMailer/archive/master.zip并上传到您的服务器。

并使用以下作为示例,在需要的地方进行更改:

<?php
require_once('/path/to/PHPMailerAutoload.php');

$mail = new PHPMailer;

$mail->isSMTP();
$mail->SMTPDebug = false;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;

$mail->Username = 'from@gmail.com';
$mail->Password = 'password';
$mail->setFrom('from@gmail.com', 'From Name (freeform string)');
$mail->addAddress('to@gmail.com'); //call this multiple times for multiple recipients
$mail->Subject = 'Subject';
$mail->msgHTML('<h3>Hello World</h3>');
$mail->AltBody = 'alternative body if html fails to load';
//$mail->addAttachment('/path/to/file/); //OPTIONAL attachment

if (!$mail->send()) {
    echo "Mailer Error: ";
    echo $mail->ErrorInfo;
} else {
    echo "Email sent";
}
?>