PHPmailer with different email address for authentication and setfrom

24 views Asked by At

I am using one Gmail account for authentication and another Gmail address in the reply-to in PHPmailer. Emails are going properly but not sure if it is a good practice in terms of email deliverability.

Your comments will be highly appreciated.

<?php
  $mail = new PHPMailer\PHPMailer\PHPMailer();
  $mail->IsSMTP();
  $mail->SMTPDebug  = 0;
  $mail->Host       = "smtp.gmail.com";
  $mail->SMTPSecure = "TLS";
  $mail->Port       = 587;
  $mail->SMTPAuth   = true;
  $mail->Username   = "[email protected]";
  $mail->Password   = "emailpassword";
  $mail->setFrom('[email protected]', 'Sender Name');
?>
1

There are 1 answers

2
Synchro On

Yes, you can do this to a limited degree with gmail.

Gmail allows you to define aliases for your account, and you can use those as from addresses in addition to the address you use for sign-in. In gmail, click the settings icon, and go to the "Accounts and import" tab. Click the "Add another email address" link and you'll get this pop-up:

Adding an alias in gmail

Enter your details and you'll have a new address that you can use as your from address.

In your PHPMailer script you'll then be able to do exactly what you're doing in your script, having different addresses in Username and From. Note that this doesn't mean that you can use arbitrary addresses as you still have to define all aliases within your account first.

If this is on a contact form, don't try to use the submitter's address as the from address; that's what reply-to is for, as the example contact form provided with PHPMailer shows.

Minor aside: $mail->SMTPSecure = "TLS"; should be $mail->SMTPSecure = "tls";.