user1032531
user1032531

Reputation: 26281

Change phpmailer's FROM address

How is phpmailer's FROM address changed? I expected the following to work, however, all emails sent use the the send from email address set by the first occurrence of SetFrom().

$mail = new myPHPMailer(true);
$mail->SMTPDebug=2;

$mail->Subject = "My Subject";
$mail->MsgHTML('My Message');
$mail->AddReplyTo('[email protected]');

$mail->ClearAllRecipients();
$mail->SetFrom('[email protected]');
$mail->AddAddress("[email protected]");
$mail->Send();

$mail->ClearAllRecipients();
$mail->SetFrom('[email protected]');  //Does not update FROM address!
$mail->AddAddress("[email protected]");
$mail->Send();

PS. Why I wish to do this? I have found that some companies set up their e-mail routers to deny all incoming external emails which have a sender email top level domain the same as their own.

Upvotes: 4

Views: 242

Answers (1)

Luceos
Luceos

Reputation: 6730

The property Sender is set once when calling the method setFrom. There is no method to individually set Sender. However you can use

$mail->Sender = <newvaluehere>;

or

$mail->set('Sender', <NEWVALUEHERE>);

Also I'd like to advice against using this library, it's hardly consistent nor does it seem production ready. You might consider a proven package like swiftmailer.

Reason why this class does not seem production ready

/**
 * Set or reset instance properties.
 * You should avoid this function - it's more verbose, less efficient, more error-prone and
 * harder to debug than setting properties directly.
 * Usage Example:
 * `$mail->set('SMTPSecure', 'tls');`
 *   is the same as:
 * `$mail->SMTPSecure = 'tls';`
 * @access public
 * @param string $name The property name to set
 * @param mixed $value The value to set the property to
 * @return boolean
 * @TODO Should this not be using the __set() magic function?
 */

Upvotes: 2

Related Questions