Setfrom Phpmailer Not Working

Setfrom Phpmailer Not Working

I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:

<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "";
$mail->Password = "gmail_password";

$mail->From         = '';
$mail->FromName     = 'Admin';
$mail->AddAddress('', 'Receiver');  // Add a recipient
$mail->IsHTML(true);

$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;  
$mail->Body = "This is in <b>Blod Text</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';
?>

It is sending mail from but I want it to send with '' as set in $mail->From. Any help will be highly appreciated.

7

5 Answers

Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.

My solution was to add

$mail->AddReplyTo($FromEmail, $FromName);

This way at least if you reply to the message it will be delivered as described

1

There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.

Use:

$mail->SetFrom('', 'Admin');

Instead of $mail->From and $mail->FromName.

0

if you want to use different email address as sentFrom, then you can set your email from gmail settings:

settings > Accounts and import > Send mail as: set another email which you want to use as from:

if you are using zoho then you can follow: settings > Mail tab > Send mail as > add from address then verify that email.

For GSuite users...

What Jyohul said, is the correct way of doing it. You can add an alias in Google Admin Console, under "Users". Click on the user's name, and then click on "user information". In there you can add Aliases. Once the aliases are added, you can then do what Jyohul said, which I added a needed step...:

if you want to use different email address as sentFrom, then you can set your email from gmail settings:

Go to your Gmail, then click the gear on the top right, then:

settings > Accounts > Send mail as: then click "Add another email address".

Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.

$mail->SMTPDebug  = 2;

You will the identify the error. Also make sure your SMTP Server credentials are correct. Like host, username and password. Mine worked correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.