thiagotalma
Sendgrid Mailer for Yii2
Sendgrid Mailer for Yii2
based on shershennm/yii2-sendgrid
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist thiagotalma/yii2-sendgrid "*"
or add
"thiagotalma/yii2-sendgrid": "*"
to the require section of your composer.json
file.
To use Mailer, you should configure it in the application configuration like the following:
Usign API Key:
'components' => [
...
'mailer' => [
'class' => 'thiagotalma\sendgrid\Mailer',
'key' => 'your api key',
//'viewPath' => '@app/views/mail', // your view path here
],
...
],
Usign username and password:
'components' => [
...
'mailer' => [
'class' => 'thiagotalma\sendgrid\Mailer',
'username' => 'your username',
'password' => 'your password here',
//'viewPath' => '@app/views/mail', // your view path here
],
...
],
To send an email, you may use the following code:
$sendGrid = Yii::$app->mailer;
$message = $sendGrid->compose('contact/html', ['contactForm' => $form])
$message->setFrom('from@domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
//also you can use sendgrid substitutions
->setSendGridSubstitution('template id', [
':var1' => 'var1value',
':var2' => 'var2value',
]);