2017-03-29 13 views
0

私は迅速なメーラー経由でメールを送信しようとしています。私はちょうど私がYouTubeでチュートリアルビデオでそれを見たように(私はここにリンクを投稿する必要がありますわからない)が、私のプロジェクトではなく、ビデオでは大丈夫です:DIはポート465と暗号化SSLで試しても、結果なし。いくつかのアドバイスをお願いします!前もって感謝します!Yii2は、迅速なメーラー経由でGmailアカウントにメールを送信します。

の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' => [ 
      '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' => '[email protected]', 
       'password' => 'password', 
       'port' => '587', 
       'encryption' => 'tls', 
      ], 
     ], 

とのparams:

return [ 
    'adminEmail' => '[email protected]', 
]; 

私はかなり遠く、それらの電子メールの物事に精通からだと私は今でもそうしてください学んでいます哀れな質問のために私に怒らないでください:)

EDIT: の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; 
    } 
} 
+0

SwiftMailer経由でメールを送信するコードを貼り付けます。 – TomaszKane

+0

このコードの外観は不明です。 –

+0

このように:https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/models/ContactForm.php#L51 – TomaszKane

答えて

1

が、私は文句を言わないbeginning.Justの用語やコードをあなたに尻込み安全性の低いアプリのためのセキュリティはGoogleアカウントにOFFであるか、それがあるnot.Ifかどうかを確認あなたのすべての設定が正しい場合、これは予期しないグリッチかもしれません。それが助けにならないなら、私に戻してください。 は設定でSMTP設定を追加した後、私は(も、私は私の設定に余分なのparamsを追加覚えていない)私の電子メールを送信するためにこれを使用:

彼らは埋めるときEMAILADDRESSは、様々なユーザーによって入力されます
$value=Yii::$app->mailer->compose() 
->setFrom('[email protected]') 
->setTo($model->emailAddress) 
->setSubject($model->subject) 
->setHtmlBody($model->content) 
->attach($model->attachment) 
->send(); 

フォームがない場合は、別の場所に電子メールを格納しておき、PHP変数でそれらをフェッチし、その変数で$ model-> emailAddressを置き換える必要があります。

+0

はい、最初は無効にしました。しかし成功なし: –

+0

私はContactFormで 'if(this-> validate())'を削除しました。問題は検証の権利にありますか? –

+0

問題は今私が私から管理者(私)にメールを送信しているだけで、私は連絡フォームに入力している電子メールではないということです。私はこれが '' username '=>' tomaivanovtomov @ gmail.com 'という行のために起こったことを理解しました。連絡先フォームのユーザーの電子メールで電子メールを置き換えるにはどうすればよいですか? –

関連する問題