【问题标题】:Why swiftmailer doesn't show senders email in yii2?为什么 swiftmailer 在 yii2 中不显示发件人电子邮件?
【发布时间】:2017-05-13 11:24:36
【问题描述】:

我这里有问题。我在我的网页中创建了一个Contact 表单,其中包括:namesurnameemaildescription。当我提交表单时,电子邮件会发送,但发件人的电子邮件是我的电子邮件。 F.e:

http://imageshack.com/a/img922/801/eVyutz.png

my.email@gmail.com 显示的不是用户在表单中输入的电子邮件,而是我的。之后,当我单击Reply button 并输入回消息时,我会将其发送给自己。为什么?

这是我所做的:

我的mailer

        'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'my.email@gmail.com',
            'password' => 'password',
            'port' => '465',
            'encryption' => 'ssl',
            'streamOptions' => [ 'ssl' =>
                [ 'allow_self_signed' => true,
                  'verify_peer' => false,
                  'verify_peer_name' => false,
                ],
            ]
        ],
    ],

我的actionContact()

public function actionContact() 
{
    $model = new Contact();
    $model->scenario = Contact::SCENARIO_CREATE;

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
            Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Success'));
        }
        else {
            Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Failed'));
        }
        return $this->refresh();
    } else {
        return $this->render('contact', [
            'model' => $model,
        ]);
    }
}

还有我的model function

public function sendEmail($email) 
{
    return Yii::$app->mailer->compose()
            ->setTo($email)
            ->setFrom([$this->email => $this->name." ".$this->surname])
            ->setSubject($this->subject)
            ->setTextBody($this->text)
            ->send();
}

请帮我解决这个问题,有什么问题吗?感谢您的帮助!

我猜$email 变量会有问题。因为当我使用setFromemail 时,它只显示我的电子邮件。

【问题讨论】:

    标签: php yii2 swiftmailer


    【解决方案1】:

    可能是您没有为 $this->email 分配适当的值(尝试检查这个,例如:使用 var_dump($this->email) 和 $this->name 和 $this->username) 您可以使用在 param.php 中分配的正确参数

      <?php
      return [
          'adminEmail' => 'my_mail@my_domain.com',
      ];
    

    .

    public function sendEmail($email) 
    {
        return Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([\Yii::$app->params['adminEmail'] => $this->name." ".$this->surname])
                // ->setFrom([\Yii::$app->params['adminEmail'] => 'try fixed test if $this->name and username not work'])           
                ->setSubject($this->subject)
                ->setTextBody($this->text)
                ->send();
    }
    

    【讨论】:

    • 我尝试将setFrom 替换为-&gt;setFrom([\Yii::$app-&gt;params['adminEmail'] =&gt; $this-&gt;name." ".$this-&gt;surname]),但这没有帮助。反正我是在自言自语:(
    • 你真的正确理解我的问题吗?发件人namesurname 显示正确,但不是他在表格中写的email,而是显示我的,无法回信
    • 和 $this->email ... 包含什么? .. var_dump($this->email); 得到了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多