Home > Blockchain >  How to specify custom sender?
How to specify custom sender?

Time:10-04

I'm using the $email->setFrom('[email protected]'); method and it's working fine for email delivered to the registered sender.

However, the email is delivered as [email protected]. How can I include the email of the customer that has filled the form? Eh: [email protected]?

If I replace $email->setFrom('[email protected]'); I will get 403.

This happen because I have to register the sender. But I just need to create a simple contact form.

Is there a way to use setFrom using a custom email?

Thanks in advance

CodePudding user response:

Twilio SendGrid developer evangelist here.

When sending emails with SendGrid you do need to verify the email or domain you are sending from. So, you can't use any email address as the from email.

You're creating a contact form, which is why you want the email to appear to come from the person sending it. However, what you really want from that is to be able to reply straight to the person that sent the email.

What you can do is send the email from a domain or email address you have verified and then set the reply-to header to the user's email address. You will receive the email from your chosen email address but when you go to reply the to address will be filled in with the custom email address.

$email->setReplyTo("[email protected]");

CodePudding user response:

<?php
/**
 * This example shows how to handle a simple contact form.
 */
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
//Don't run this unless we're handling a form submission
if (array_key_exists('email', $_POST)) {
    date_default_timezone_set('Etc/UTC');
    require '../assets/import/PHPMailer/PHPMailerAutoLoad.php';
    //Create a new PHPMailer instance
    $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP - requires a local mail server
    //Faster and safer than using mail()
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->Port = 25;
    //Use a fixed address in your own domain as the from address
    //**DO NOT** use the submitter's address here as it will be forgery
    //and will cause your messages to fail SPF checks
    $mail->setFrom('[email protected]', 'First Last');
    //Send the message to yourself, or whoever should receive contact for submissions
    $mail->addAddress('[email protected]', 'John Doe');
    //Put the submitter's address in a reply-to header
    //This will fail if the address provided is invalid,
    //in which case we should ignore the whole request
    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'PHPMailer contact form';
        //Keep it simple - don't use HTML
        $mail->isHTML(false);
        //Build a simple message body
        $mail->Body = <<<EOT
        Email: {$_POST['email']}
        Name: {$_POST['name']}
        Message: {$_POST['message']}
        EOT;
        //Send the message, check for errors
        if (!$mail->send()) {
            //The reason for failing to send will be in $mail->ErrorInfo
            //but you shouldn't display errors to users - process the error, log it on your server.
            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }
}
?>

  • Related