Chào các bạn hôm nay mình sẽ giới thiệu đến với các bạn send mail trong yii2 với smtp đăng kí từ amazon và sử dụng gmail
1 Đối với amazon
Bạn vào https://console.aws.amazon.com/iam/home?#/users tạo 1 user smtp mới với các thông số cần thiết là username và password
Ở đây của mình là thông số : username : AKIAIHU4WS35XGTV**** và password là Ant3Ae6GP6abbxOvmddI6+B7wLpR5nzpQNEY5xg8**** .Sau đó bạn vào file common/config/main-local.php thêm các thông số như sau vào
<?php return [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=demoyii2', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/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' => true, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'email-smtp.us-east-1.amazonaws.com', 'username' => 'AKIAIHU4WS35XGTVAOUQ', 'password' => 'Ant3Ae6GP6abbxOvmddI6+B7wLpR5nzpQNEY5xg******', 'port' => '587', 'encryption' => 'tls', ], ], ], ];
Với các thông số đó chúng ta đã kết nối thành công ses của amazon với yii2 .Bước tiếp theo
chúng ta test các thông số
$aa = \Yii::$app->mailer->compose ()->setFrom ( 'shang9x@gmail.com' ) ->setTo ( 'shang912312312421x@gmail.com' ) ->setSubject ( 'Mail gửi cho hiến ' ) ->setTextBody ( 'Đây là mail gửi tự động cho nguyễn anh hiến' . date ( "Y/m/d" ) . '' ) ->send ();
Lưu ý các bạn cần xác thức email trong amazon trước để có thể gửi mail thành công .
Demo nhỏ của mình để các bạn có thể demo việc gửi mail ở link :https://demo.hbsprogram.com/sendmail
Ở ví dụ này mình tạo các thành phần gồm
Models có file Sendemail.php ở folder frontend/model
<?php namespace frontend\models; use yii\base\Model; class Sendmail extends Model { public $form_input; public function rules() { return [ ['form_input','required'], ['form_input','email'], ['form_input','trim' ], ]; } public function attributeLabels() { return[ 'form_input'=>'Nhập vào email của bạn', ]; } }
Controllers ở mục frontend/controllers như sau
<?php namespace frontend\controllers; use yii\base\Controller; use frontend\models\Sendmail; class SendmailController extends Controller { public function actionIndex() { $model = new Sendmail(); if ($model->load(\Yii::$app->request->post()) & $model->validate()) { $aa = \Yii::$app->mailer->compose () ->setFrom ( 'anhhienbadao@gmail.com' ) ->setTo ($model->form_input) ->setSubject ( 'Test mail ses ở HBSPROGRAM.COM' ) ->setTextBody ( 'Đây là ví dụ về sendmail với amazon ses của trang hbsprogram.com' ) ->send (); return $this->render('thongbao',['model'=>$model]); } else { return $this->render('nhap',['model'=>$model]); } } }
Ở file view mình có tạo 2 file view đơn giản nhằm thông báo với người dùng đã gửi mail thành công là nhap.php
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin()?> <?= $form->field($model, 'form_input')->textInput()?> <div class="form-group"> <?= Html::submitButton('Send mail', ['class' => 'btn btn-primary'])?> </div> <?php $form->end()?>
Và 1 file thông báo thành công sau khi gửi mail