0

I want to send emails through my host email that is : admin@b4p.et

I have passing this email in from email id but this is sending mails from the SMTP username which i have configured in common/config/main.php

Following is my mail configurations in common/config/main.php :

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'chiefsrsmail@gmail.com',
            'password' => '*********',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],

And following is my send mail function :

 public static function sendMailToUser($ssToEmail, $asFromEmail, $ssSubject, $ssBody, $attach = false)
    {
        $result = Yii::$app->mail->compose()
            ->setFrom([$asFromEmail])
            ->setTo($ssToEmail)
            ->setSubject($ssSubject)
            ->setHtmlBody($ssBody)
            ->send();
        return true;
    }

In which i am passing :

$asFromEmail = admin@b4p.et

This function works fine for sending emails but the all emails are going through "chiefsrsmail@gmail.com" means all emails takes from email address from SMTP username which i have provided in configuration.I want to change it but it is not working.

Please help me to get out of this.

Sfili_81
  • 2,377
  • 8
  • 27
  • 36
php guy
  • 113
  • 13

1 Answers1

1

Gmail automatically rewrites the "from" header of any messages sent through its SMTP server to the default "Send mail as" email address assigned in the Gmail or Google Apps account used to authenticate (in this case, your personal account).

Gmail doesn't seem to allow sending the email via a different email-id if its not registered to the primary gmail account.

This SMTP service is intended for personal use only, so its not very flexible. We can change this address by modifying the default account in the Gmail settings, but this still won't enable us to configure the "from" address through the application.

To add another email to Gmail account follow below steps :

On your computer, open Gmail.

  • In the top right, click Settings Settings and then Settings.
  • Click the Accounts and import or Accounts tab.
  • In the "Send mail as" section, click Add another email address.
  • Enter your name and the address you want to send from.
  • Click Next Step and then Send verification.
  • Click Add Account.

Confirm the link sent to the additional email account. On confirmation, your account can send emails from either of the address.

Try the new email address now in

->SetFrom("verified_mail_address@gmail.com", 'from_name'); and it will work fine.

To add another email to Gmail account refer this link : https://support.google.com/mail/answer/22370?hl=en

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53