yii2-sendgrid
Sendgrid Mailer for Yii2, (*1)
based on shershennm/yii2-sendgrid, (*2)
, (*3)
Installation
The preferred way to install this extension is through composer., (*4)
Either run, (*5)
php composer.phar require --prefer-dist thiagotalma/yii2-sendgrid "*"
or add, (*6)
"thiagotalma/yii2-sendgrid": "*"
to the require section of your composer.json file., (*7)
Usage
To use Mailer, you should configure it in the application configuration like the following:, (*8)
Usign API Key:, (*9)
'components' => [
...
'mailer' => [
'class' => 'thiagotalma\sendgrid\Mailer',
'key' => 'your api key',
//'viewPath' => '@app/views/mail', // your view path here
],
...
],
Usign username and password:, (*10)
'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:, (*11)
$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',
]);