可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php require 'phpmailer/class.phpmailer.php'; $mail = new PHPMailer; $mail->IsSMTP(); $mail->SMTPAuth = true; $mail->Host = "smtp.gmail.com"; $mail->Port = 587; $mail->Username = "myusername@gmail.com"; $mail->Password = "gmail_password"; $mail->From = 'donotreply@mydomain.com'; $mail->FromName = 'Admin'; $mail->AddAddress('Toreceiver@test.com', 'Receiver'); // Add a recipient $mail->IsHTML(true); $mail->Subject = 'Here is the Subject'; $mail->WordWrap = 50; $mail->Body = "This is in <b>Blod Text</b>"; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->Send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } echo 'Message has been sent'; ?>
It is sending mail from myusername@gmail.com but I want it to send with 'donotreply@mydomain.com' as set in $mail->From. Any help will be highly appreciated.
回答1:
Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.
My solution was to add
$mail->AddReplyTo($FromEmail, $FromName);
This way at least if you reply to the message it will be delivered as described
回答2:
There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.
Use:
$mail->SetFrom('donotreply@mydomain.com', 'Admin');
Instead of $mail->From and $mail->FromName.
回答3:
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
settings > Accounts and import > Send mail as: set another email which you want to use as from:
if you are using zoho then you can follow: settings > Mail tab > Send mail as > add from address then verify that email.
回答4:
Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.
$mail->SMTPDebug = 2;
You will the identify the error. Also make sure your SMTP Server credentials are correct. Like host, username and password. Mine worked correctly