php 如何使用phpmailer更改信封发件人地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13918999/
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 06:15:55  来源:igfitidea点击:

How to change envelope sender address using phpmailer?

phpphpmailer

提问by PJ Bergeron

With php mail() I can write

用 php mail() 我可以写

mail('[email protected]','subject!','body','From: [email protected]','-f [email protected]');

But how can I do the same with phpmailer ?

但是我怎么能用 phpmailer 做同样的事情呢?

回答by Hannes Morgenstern

The relevant line in Theolodis answer is:

Theolodis 回答中的相关行是:

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

There is no need to use AddReplyTo()this is something completely different.

没有必要使用AddReplyTo()这是完全不同的东西。

You only need to set your from address (and name optionally) by using SetFrom(). If you look at the code, SetFrom()takes three parameters:

您只需要使用SetFrom(). 如果你看代码SetFrom()需要三个参数:

/**
 * Set the From and FromName properties
 * @param string $address
 * @param string $name
 * @param boolean $auto Whether to also set the Sender address, defaults to true
 * @throws phpmailerException
 * @return boolean
 */
public function SetFrom($address, $name = '', $auto = true) {
....

the third parameter (defaults to true) and therefor the envelope sender gets set to the same address as the sender.

第三个参数(默认为 true),因此信封发件人被设置为与发件人相同的地址。

It gets interesting if you want to set different addresses as envelope sender and From Address. This is the way how to CHANGEenvelope sender. Therefor you have to set the $senderproperty of your PHPMailerinstance like this:

如果您想将不同的地址设置为信封发件人和发件人地址,这会很有趣。这是如何更改信封发件人的方法。因此,您必须像这样设置实例的$sender属性PHPMailer

  $pMail->Sender='[email protected]';
  $pMail->SetFrom('[email protected]', 'First Last', FALSE);

回答by Theolodis

This example shows how.

这个例子展示了如何。

the relevant lines:

相关线路:

$mail->SetFrom('[email protected]', 'First Last');
$mail->AddReplyTo('[email protected]', 'First Last');

回答by Ben Kucenski

The -f flag is set with $email->Sender

-f 标志设置为 $email->Sender

This is the Envelope From which allows the email server to evaluate the sender's email address before receiving the rest of your email data

这是信封,允许电子邮件服务器在收到您的其余电子邮件数据之前评估发件人的电子邮件地址

SetFrom - this is what email address the end user will see as the message coming from

SetFrom - 这是最终用户将看到的邮件地址

AddReplyTo - this is what email address will pop up if they reply to the email

AddReplyTo - 这是他们回复电子邮件时将弹出的电子邮件地址

Sender needs to be clean to pass spam filters

发件人需要干净才能通过垃圾邮件过滤器

SetFrom needs to be clean to pass spam filters

SetFrom 需要干净才能通过垃圾邮件过滤器

AddReplyTo doesn't really matter. This allows a service to send an email on behalf of a user with an email address not managed by the service.

AddReplyTo 并不重要。这允许服务代表具有不受服务管理的电子邮件地址的用户发送电子邮件。

回答by zzapper

What worked for me (obscurely) using

什么对我有用(默默地)使用

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

was to use localhost rather than directly access the smtp server e.g.

是使用 localhost 而不是直接访问 smtp 服务器,例如

$email->Host='localhost'; // SMTP server this way you get from name (don't know why)

Now mails arrive in Outlook from 'Rupert Bear' [[email protected]]

现在邮件从“Rupert Bear”[[email protected]] 到达 Outlook