dpca31461 2017-03-29 08:10
浏览 93
已采纳

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

I am trying to send email via swift mailer.I know there is a lot if similar questions but can't understand where is my problem. I did it just like i saw it in tutorial video in youtube (don't know shall i post the link here) but like always it is ok on the video but not on my project :D I tried with port 465 and encryption ssl also but without result. Will please you for some advice! Thank you in advance!

actionContact:

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 config:

'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',
            ],
        ],

and params:

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

I am pretty far from familiar with those emails things and i am still learning so please do not get mad at me for the pathetic question :)

EDIT: ContactForm model:

<?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;
    }
}
  • 写回答

1条回答 默认 最新

  • dpge74512 2017-03-29 13:24
    关注

    I wont boggle you with terms and code at the beginning.Just check whether security for less secure apps is OFF in your google account or not.If it is ON then turn it OFF.If all your settings are correct then this could be the unexpected glitch. Revert to me if that doesn't help. After adding the smtp configuration in config I used this to send my emails(also I don't remember adding any extra params to my config):

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

    where the emailAddress will be filled by various users when they fill out the form, if u have one.If that is not the case and you have the emails stored elsewhere then you will have to fetch them in a php variable and replace $model->emailAddress with that variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题