Reputation: 143
I'm upgrading to phpmailer 6 and testing my forms.
If I use the following code, it doesn't work:
$mail = new PHPMailer;
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$mail->Sender = '[email protected]';
$mail->setFrom($_POST['email'], $_POST['name'], false);
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress($to);
I have tried various combinations with setFrom (e.g. removing the boolean flag, omitting the $mail->Sender piece, omitting the addReplyTo) and it never works when using setFrom().
However, if I use this code, it does appear to work:
$mail = new PHPMailer;
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->addAddress($to);
Any idea why the setFrom() doesn't send the email?
Upvotes: 0
Views: 78
Reputation: 37760
You have not said what you mean by "doesn't work". Does it throw errors, not send at all, or what? It would help to see an SMTP transcript (set SMTPDebug = 2
).
Problems aside, this is a bad idea:
$mail->setFrom($_POST['email'], $_POST['name'], false);
This is forgery, and will cause your messages to be rejected or spam-filtered because it will break DMARC alignment. Setting $mail->Sender = '[email protected]';
will help a bit as your envelope sender will be ok, but generally speaking don't do this as it's not going to help with deliverability.
One difference that setFrom()
has over setting the From
property directly is that it verifies the address immediately, and you can see the result - try this:
if (!$mail->setFrom($_POST['email'], $_POST['name'], false)) {
die('bad address');
}
If you give it a bad address, then sending will fail, whereas it may still be attempted if you have set the property directly - this may explain the difference you're seeing.
I would advise you to set it up this way, that is not forging either the from address or the envelope sender, but still making replies to form submissions go to the submitter:
setFrom('[email protected]');
if (!$mail->addReplyTo($_POST['email'], $_POST['name'])) {
die('bad address');
}
Upvotes: 1