Email Id : phpmk888@gmail.com
yii2 swiftmailer Check SMTP Connection
I need to check a smtp connection with username and password (without sending an actual email).
After added smtpmail extension file into extension folder, We have to give the set of details like Hostname, Username, Password, Mailer option, Port and SMTPAuth. We must have to configure this details in main.php before send the mail using this extension. I added the code below
Finally i came with this solution
use Swift;
use Swift_Message;
use Swift_Mailer;
use Swift_SmtpTransport;
class AccountController extends Controller {
public function actionSmtpSetting() {
$model = new ModelSMTP();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->save();
return $this->redirect(['index']);
} else {
return $this->render('smtp-setting', [
'model' => $model
]);
}
}
public function actionTestsmtpconnection() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post('DynamicModel');
$smtp_host_name = $post['smtp_host_name'];
$smpt_user_name = $post['smpt_user_name'];
$smtp_password = $post['smtp_password'];
$smtp_from_email = $post['smtp_from_email'];
try {
$transport = Swift_SmtpTransport::newInstance($smtp_host_name, '587', 'tls');
$transport->setUsername($smpt_user_name);
$transport->setPassword($smtp_password);
$mailer = \Swift_Mailer::newInstance($transport);
$mailer->getTransport()->start();
return 'ok';
} catch (Swift_TransportException $e) {
return $e->getMessage();
} catch (Exception $e) {
return $e->getMessage();
}
}
exit;
}
}
This is View With Ajax Validation....
$form = ActiveForm::begin(['enableClientValidation' => true, 'enableAjaxValidation' => false,
'fieldConfig' => [
'template' => "
{label}
{input}{error}
"
],
'options' => ['enctype' => 'multipart/form-data',
'class' => 'form-horizontal1',
'id' => 'smtp-create']]);
?>
field($model, 'smtp_host_name')->textInput(['maxlength' => true, 'value' => $smtp_host_name->value, 'readonly' => $flag]) ?>
field($model, 'smpt_user_name')->textInput(['maxlength' => true, 'value' => $smpt_user_name->value, 'readonly' => $flag]) ?>
field($model, 'smtp_password')->passwordInput(['maxlength' => true, 'value' => $smtp_password->value, 'readonly' => $flag]) ?>
field($model, 'smtp_from_email')->textInput(['maxlength' => true, 'value' => $smtp_from_email->value, 'readonly' => $flag]) ?>
'']); ?>
session['organizationSmtp']) { ?>
'disabled', 'id' => 'testsmtp-connection', 'class' => 'btn btn-primary block full-width m-b']) ?>
'testsmtp-connection', 'class' => 'btn btn-primary block full-width m-b']) ?>
session['organizationSmtp']) { ?>
'disabled', 'id' => 'form-submit-btn', 'class' => 'disabled btn btn-primary block full-width m-b']) ?>
'disabled', 'id' => 'form-submit-btn', 'class' => 'disabled btn btn-primary block full-width m-b']) ?>
request->getCsrfToken();
$actionSubmit = Yii::$app->request->baseUrl . '/account/testsmtpconnection';
$successMsg = Yii::t("OrgSettings","Test connection has been successfully.");
$errorMsg = Yii::t("OrgSettings","There may a error on submitting. Try again later.");
$script = <<< JS
$(document).ready(function () {
$("#smtp-create").on('beforeSubmit', function (event) {
event.preventDefault();
var form_data = new FormData($('#smtp-create')[0]);
$.ajax({
url: '$actionSubmit',
dataType: 'html',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
beforeSend: function() {
$('#progress-wrp').show();
},
success: function(response){
if(response=='ok'){
$('#dynamicmodel-test_valid').val('ok');
$('#form-submit-btn').prop('disabled', false);
$("#form-submit-btn").removeClass("disabled");
$("#testsmtp-connection").addClass("disabled");
$('#error-message').html('
$successMsg
');
}else{
if($('#form-submit-btn').is('[disabled=disabled]')){
$('#error-message').html('
'+response+'
');
}
}
},
complete: function() {
$('#progress-wrp').hide();
},
error: function (data) {
if($('#form-submit-btn').is('[disabled=disabled]')){
$('#error-message').html('
$errorMsg
');
}
}
});
if($('#form-submit-btn').is('[disabled=disabled]')){
return false;
}
});
});
JS;
$this->registerJs($script);
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
ReplyDeleteHire PHP Programmer in India