邮件由gmail单独通过php mail()以及smtp

时间:2016-01-08 05:29:53

标签: php smtp phpmailer email-spam

我试图发送电子邮件以使用php mail()重置用户密码,但它只能在Gmail帐户中发送到垃圾邮件文件夹中的yahoo邮件我可以在收件箱中收到此邮件。我用Google搜索并看到有些人说通过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邮件程序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!";
}

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错,但如果您的服务器的IP地址已添加到用于过滤垃圾邮件的垃圾邮件黑名单中,那么从您的服务器发送邮件的方式无关紧要。

Google搜索类似"邮件服务器黑名单"并测试您的服务器的IP地址,看它是否已被列入黑名单。如果您从托管公司租用服务器,则更改后您的IP地址已被其他人使用。

您可以尝试为贵公司创建测试Gmail帐户,然后尝试使用该Gmail帐户通过SMTP发送和发送电子邮件,并查看是否已发送电子邮件。电子邮件将代表您从Gmail服务器发送。

另一种选择是使用像Mailgin这样的邮件API服务。