Yii2 通过 swift 邮件程序向 gmail 帐户发送电子邮件

Yii2 sending email to gmail account via swift mailer

我正在尝试通过 swift 发送电子邮件 mailer.I 知道有很多类似的问题,但不明白我的问题在哪里。我做的就像我在 youtube 的教程视频中看到的那样(不知道我应该 post 还是 link 在这里)但是像往常一样它在视频上没问题但在我的项目上没问题 :D 我试过了端口 465 和加密 ssl 也没有结果。会请你一些建议!提前致谢!

行动联系人:

public function actionContact()
    {
        $model = new ContactForm();

        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

邮件程序配置:

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@app/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'tomaivanovtomov@gmail.com',
                'password' => 'password',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],

和参数:

return [
    'adminEmail' => 'tomaivanovtomov@gmail.com',
];

我对那些电子邮件的事情还很不熟悉,我还在学习所以请不要因为这个可悲的问题而生我的气:)

编辑: ContactForm 型号:

<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends Model
{
    public $name;
    public $email;
    public $title;
    public $body;
    public $verifyCode;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['name', 'email', 'title', 'body'], 'required'],
            // email has to be a valid email address
            ['email', 'email'],
            // verifyCode needs to be entered correctly
            ['verifyCode', 'captcha'],
        ];
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'name' => 'Name:',
            'email' => 'Email:',
            'title' => 'Title:',
            'body' => 'Connect with us :)',
            'verifyCode' => 'Verification Code'
        ];
    }

    /**
     * Sends an email to the specified email address using the information collected by this model.
     * @param string $email the target email address
     * @return bool whether the model passes validation
     */
    public function contact($email)
    {
        if ($this->validate()) {
            Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([$this->email => $this->name])
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send();

            return true;
        }
        return false;
    }
}

我不会用 beginning.Just 中的条款和代码让你困惑,检查你的 google 帐户中安全性较低的应用程序的安全性是否已关闭,或者 not.If 它是否已打开,然后将其打开 OFF.If 您的所有设置都正确,那么这可能是意外故障。如果这没有帮助,请回复我。 在配置中添加 smtp 配置后,我用它来发送我的电子邮件(我也不记得在我的配置中添加了任何额外的参数):

 $value=Yii::$app->mailer->compose()
->setFrom('tomaivanovtomov@gmail.com')
->setTo($model->emailAddress)
->setSubject($model->subject)
->setHtmlBody($model->content)
->attach($model->attachment)
->send();

不同的用户在填写表单时会填写 emailAddress,如果您有 one.If 情况并非如此,并且您将电子邮件存储在其他地方,那么您将不得不在一个php 变量并将 $model->emailAddress 替换为该变量。