【发布时间】:2016-04-12 17:27:10
【问题描述】:
我尝试使用 php mail() 发送电子邮件以重置用户密码,但它仅在 yahoo 邮件中发送到 Gmail 帐户中的垃圾邮件文件夹,我可以在收件箱中收到此邮件。我用谷歌搜索,看到有些人说要通过 phpmailer 使用 smtp 邮件服务器。
我也试过了,但我得到了与 php mail() 函数相同的结果.. 这里是我的 php mail() 函数代码
$headers = 'From: no-reply@mydomain.us' . "\r\n";
$headers .= 'Reply-To: '.$to.'';
$headers .= 'Subject: ' . $subject ." \r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset="iso-8859-1"'. "\r\n";
@mail($to, $subject, $msg, $headers);
和php mailer smtp代码
date_default_timezone_set('Etc/UTC');
require_once 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'mydomain.us';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "cs@mydomain.us";
//Password to use for SMTP authentication
$mail->Password = "pass";
//Set who the message is to be sent from
$mail->setFrom('cs@umydomain.us.us', 'sender name');
//Set an alternative reply-to address
$mail->addReplyTo('cs@mydomain.us', 'sender name');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($msg);
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
【问题讨论】:
-
您的邮件是否最终成为垃圾邮件与您发送它的方式没有太大关系,而与您发送的内容和收件人的处理方式有关。如果收件人将其标记为垃圾邮件,不久之后,gmail 将默认开始这样做。 Gmail 有将几乎所有内容标记为垃圾邮件的历史,而且情况越来越糟。
标签: php smtp phpmailer email-spam