Di kesempatan kali ini kita akan membahas cara untuk membuat form email dan memberikan pilihan untuk menyertakan attachment pada email tersebut.
1. Buat tabel email dengan atribut ini :
2. Buat model dan crud menggunakan gii pada tabel email.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<?php namespace backend\models; use Yii; /** * This is the model class for table "emails". * * @property integer $id * @property string $receiver_name * @property string $receiver_email * @property string $subject * @property string $content * @property string $attachment */ class Emails extends \yii\db\ActiveRecord { /** * @inheritdoc */ public static function tableName() { return 'emails'; } /** * @inheritdoc */ public function rules() { return [ [['receiver_name', 'receiver_email', 'subject', 'content', 'attachment'], 'required'], [['content'], 'string'], [['receiver_name'], 'string', 'max' => 50], [['receiver_email'], 'string', 'max' => 200], [['subject', 'attachment'], 'string', 'max' => 255] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'receiver_name' => 'Receiver Name', 'receiver_email' => 'Receiver Email', 'subject' => 'Subject', 'content' => 'Content', 'attachment' => 'Attachment', ]; } } |
3. Tambahkan mailer di dalam config/main.php
1 2 3 4 |
'mailer'=>[ 'class'=>'yii\swiftmailer\Mailer', 'useFileTransport'=>false, ], |
4. Modifikasi file _form.php dengan menambahkan tipe input fileInput untuk attachment dan jangan lupa untuk menambahkan enctype multipart/form-data untuk activeform :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model backend\models\Emails */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="emails-form"> <?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?> <?= $form->field($model, 'receiver_name')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'receiver_email')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'subject')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?> <?= $form->field($model, 'attachment')->fileInput(['maxlength' => true]) ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div> |
5. Modifikasi EmailController pada actionCreate dan jangan lupa tambahkan use yii\web\UploadedFile :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public function actionCreate() { $model = new Emails(); if ($model->load(Yii::$app->request->post())) { $model->attachment = UploadedFile::getInstance($model,'attachment'); if($model->attachment) { $time = time(); $model->attachment->saveAs('attachments/'.$time.'.'.$model->attachment->extension); $model->attachment='attachments/'.$time.'.'.$model->attachment->extension; } if($model->attachment) { $value = Yii::$app->mailer->compose() ->setTo($model->receiver_email) ->setSubject($model->subject) ->setHtmlBody($model->content) ->attach($model->attachment) ->send(); }else { $value = Yii::$app->mailer->compose() ->setTo($model->receiver_email) ->setSubject($model->subject) ->setHtmlBody($model->content) ->send(); } $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } } |
6. Jangan lupa untuk membuat folder baru bernama attachments di dalam folder web.
Tutorial ini sebagai dokumentasi dan pembelajaran pribadi sekalian belajar terjemahin bahasa Inggris, dan siapa tahu bermanfaat buat orang lain. Sumber lengkapnya diambil dari Youtube DoingITEasy Channel.
Saya ikutin langkah seperti diatas gan, tpi kenapa ngga bisa ya ? terus muncul peringatan (Swift_TransportException
Expected response code 220 but got code “”, with message “”)
Apa ada settingan tambahan?
Oh, error swift ya? Kemungkinan error dari smtp google nya. Coba daftar smtp2go.com, gratis kok. Terus ikuti tutorial yang ini : http://indocoder.com/tips-trick-yii2/yii2-tips-22-mengirim-email/
Saya ikutin langkah seperti diatas gan, tpi kenapa ngga bisa ya ? terus muncul peringatan
Swift_TransportException: Process could not be started [The system cannot find the path specified.
] in C:\wamp64\www\advanced\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php:299
Stack trace:
#0 C:\wamp64\www\advanced\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php(54): Swift_Transport_StreamBuffer->establishProcessConnection()
#1 C:\wamp64\www\advanced\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php(143): Swift_Transport_StreamBuffer->initialize(Array)
#2 C:\wamp64\www\advanced\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\SendmailTransport.php(50): Swift_Transport_AbstractSmtpTransport->start()
#3 C:\wamp64\www\advanced\vendor\yiisoft\yii2-swiftmailer\src\Mailer.php(113): Swift_Transport_SendmailTransport->start()
#4 C:\wamp64\www\advanced\vendor\yiisoft\yii2-swiftmailer\src\Mailer.php(156): yii\swiftmailer\Mailer->getSwiftMailer()
#5 C:\wamp64\www\advanced\vendor\yiisoft\yii2\mail\BaseMailer.php(263): yii\swiftmailer\Mailer->sendMessage(Object(yii\swiftmailer\Message))
#6 C:\wamp64\www\advanced\vendor\yiisoft\yii2\mail\BaseMessage.php(49): yii\mail\BaseMailer->send(Object(yii\swiftmailer\Message))
#7 C:\wamp64\www\advanced\backend\controllers\EmailController.php(83): yii\mail\BaseMessage->send()
#8 [internal function]: backend\controllers\EmailController->actionCreate()
#9 C:\wamp64\www\advanced\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#10 C:\wamp64\www\advanced\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#11 C:\wamp64\www\advanced\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction(‘create’, Array)
#12 C:\wamp64\www\advanced\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction(’email/create’, Array)
#13 C:\wamp64\www\advanced\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#14 C:\wamp64\www\advanced\backend\web\index.php(17): yii\base\Application->run()
#15 {main}
Copy Stacktrace Search Stackoverflow Search Google Exception
Swift_TransportException
Process could not be started [The system cannot find the path specified.
]
Kemungkinan error dari smtp google nya. Coba daftar smtp2go.com, gratis kok. Terus ikuti tutorial yang ini : http://indocoder.com/tips-trick-yii2/yii2-tips-22-mengirim-email/