简体   繁体   English

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

[英]How to change envelope sender address using phpmailer?

With php mail() I can write用 php mail() 我可以写

mail('to@example.com','subject!','body','From: from@example.com','-f from@example.com');

But how can I do the same with phpmailer ?但是我怎么能用 phpmailer 做同样的事情呢?

The relevant line in Theolodis answer is: Theolodis 回答中的相关行是:

$mail->SetFrom('name@yourdomain.com', '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() .您只需要使用SetFrom()设置您的SetFrom()地址(和名称可选SetFrom() If you look at the code , SetFrom() takes three parameters:如果你看代码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 CHANGE envelope sender.这是如何更改信封发件人的方法。 Therefor you have to set the $sender property of your PHPMailer instance like this:因此,您必须像这样设置PHPMailer实例的$sender属性:

  $pMail->Sender='admin@yourdomain.com';
  $pMail->SetFrom('name@yourdomain.com', 'First Last', FALSE);

This example shows how. 这个例子展示了如何。

the relevant lines:相关行:

$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo('name@yourdomain.com', 'First Last');

As Hannes Morgenstern correctly suggested, the answer is:正如Hannes Morgenstern正确建议的那样,答案是:

$pMail->Sender='admin@yourdomain.com';
$pMail->SetFrom('name@yourdomain.com', 'First Last', FALSE);

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. AddReplyTo 并不重要。 This allows a service to send an email on behalf of a user with an email address not managed by the service.这允许服务代表具有不受服务管理的电子邮件地址的用户发送电子邮件。

What worked for me (obscurely) using什么对我有用(默默地)使用

$mail->SetFrom('name@yourdomain.com', 'Rupert Bear');

was to use localhost rather than directly access the smtp server eg是使用 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' [name@yourdomain.com]现在邮件从“Rupert Bear”[name@yourdomain.com] 到达 Outlook

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM