php SetFrom PHPMailer 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17085865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:14:00  来源:igfitidea点击:

SetFrom PHPMailer not working

phpphpmailer

提问by Prem Baranwal

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:

我正在使用 gmail SMTP 在 phpmailer 库的帮助下发送邮件。它发送邮件很好,但不是从我在 SetFrom 地址中设置的邮件地址发送。这是我的代码:

<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "[email protected]";
$mail->Password = "gmail_password";

$mail->From         = '[email protected]';
$mail->FromName     = 'Admin';
$mail->AddAddress('[email protected]', '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 [email protected] but I want it to send with '[email protected]' as set in $mail->From. Any help will be highly appreciated.

它从 [email protected] 发送邮件,但我希望它使用 $mail->From 中设置的“[email protected]”发送。任何帮助将不胜感激。

回答by Senixon

Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.

是的,这是 Google Mail 限制。“发件人”电子邮件地址必须匹配或由 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

这样,至少如果您回复消息,它将按照描述进行传递

回答by JarnoV

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.

在 phpmailer 中还有另一种设置 from address 的方法,它得到了更好的支持。我发现我使用 phpmailer 的服务器没有将电子邮件从同一主机传递到另一台服务器。但是后来我改变了设置发件人地址的方式并解决了问题。

Use:

用:

$mail->SetFrom('[email protected]', 'Admin');

Instead of $mail->From and $mail->FromName.

而不是 $mail->From 和 $mail->FromName。

回答by Jyohul

if you want to use different email address as sentFrom, then you can set your email from gmail settings:

如果您想使用不同的电子邮件地址作为 sentFrom,那么您可以从 Gmail 设置中设置您的电子邮件:

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.

如果您使用的是 zoho,那么您可以按照以下步骤操作:设置 > 邮件选项卡 > 发送邮件为 > 添加发件人地址,然后验证该电子邮件。

回答by James Erikson

For GSuite users...

对于 G Suite 用户...

What Jyohul said, is the correct way of doing it. You can add an alias in Google Admin Console, under "Users". Click on the user's name, and then click on "user information". In there you can add Aliases. Once the aliases are added, you can then do what Jyohul said, which I added a needed step...:

Jyohul 所说的是正确的做法。您可以在 Google 管理控制台中的“用户”下添加别名。单击用户名,然后单击“用户信息”。在那里你可以添加别名。添加别名后,您就可以执行 Jyohul 所说的操作,我添加了一个必要的步骤...:

if you want to use different email address as sentFrom, then you can set your email from gmail settings:

如果您想使用不同的电子邮件地址作为 sentFrom,那么您可以从 Gmail 设置中设置您的电子邮件:

Go to your Gmail, then click the gear on the top right, then:

转到您的 Gmail,然后点击右上角的齿轮,然后:

settings > Accounts > Send mail as: then click "Add another email address".

设置 > 帐户 > 发送邮件:然后单击“添加另一个电子邮件地址”。

回答by Samuel Dervis

Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.

升级到最新版本的 PHPMailler。您还应该确保在 oder 中打开调试以查看错误消息。

$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

您将识别错误。还要确保您的 SMTP 服务器凭据正确无误。像主机,用户名和密码。我的工作正常