简体   繁体   English

Yii2 发送邮件:来自 email 未显示

[英]Yii2 sending mail : from email is not displaying

I want to send emails through my host email that is: admin@b4p.et我想通过我的主机 email 发送电子邮件,即: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 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:以下是我在 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:以下是我发送的邮件 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.此 function 可以正常发送电子邮件,但所有电子邮件都通过“chiefsrsmail@gmail.com”表示所有电子邮件都来自 email 地址,地址来自 ZC2239A92BDE29F0A9F9173193CC2FE0,但它提供的用户名无效。

Please help me to get out of this.请帮助我摆脱困境。

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 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.如果 Gmail 未注册到主 gmail 帐户,Gmail 似乎不允许通过不同的电子邮件 ID 发送 email。

This SMTP service is intended for personal use only, so its not very flexible.此 SMTP 服务仅供个人使用,因此不太灵活。 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.我们可以通过修改 Gmail 设置中的默认帐户来更改此地址,但这仍然无法让我们通过应用程序配置“发件人”地址。

To add another email to Gmail account follow below steps:要将另一个 email 添加到 Gmail 帐户,请按照以下步骤操作:

On your computer, open Gmail.在您的计算机上,打开 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.在“发送邮件为”部分中,单击添加另一个 email 地址。
  • 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.确认发送到附加 email 帐户的链接。 On confirmation, your account can send emails from either of the address.确认后,您的帐户可以从任一地址发送电子邮件。

Try the new email address now in现在尝试新的 email 地址

->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要将另一个 email 添加到 Gmail 帐户,请参阅此链接: https://support.google.com/mail/answer/22370?hl=en

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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