简体   繁体   中英

Using PHP Mailer to send smtp emails from an email address outside the local server

I want to use PHPMailer configure the contact page for my website. The prroblem I am facing is that for the script to work, the from address has to be set in the local server eg if my domain is example.com , the from address has to be name@example.com . When I set it this way, the script works perfectly. the issue I'm having is that being a contact page, the from address has to be the senders email address this there for means that if a customer contacts me with the email address like name@email.com, the script will throw an error similar to the one below:

SMTP ERROR: DATA END command failed:
550-Your FROM address ( name@email.com , Dev Customer 550-)
must match your authenticated email user ( name@example.com ).

Does anyone have any idea on a way to work around this? Or are there any alternatives? I will appreciate any form of assistance offered. Thank you in advance.

As per Tigger's suggestion, you should never send using the submitter's address as a From address. It's forgery, and will make your messages fail SPF checks, causing you no end of delivery problems either with messages bouncing or consigned to spam folders. It's been actively advocated against for at least a decade anyway, and it's also one of the practices that would cause you to be vulnerable to the security hole in PHPMailer fixed back in December.

Since you've tagged this with PHPMailer, I'll show how to use that to do what you ask:

$mail->setFrom('me@example.com', 'Contact form');
$mail->addReplyTo($_POST['email'], $_POST['name']);

This is exactly what the example contact form script provided with PHPMailer does. It's also worth checking submitted values - addReplyTo will validate the address automatically, so you should check the return value to be sure.

There may be some email clients that do not handle reply-to addresses correctly, but you should not expose yourself to being blacklisted because of the ineptitude of a small number of clients.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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