PHP code example of nyx-solutions / yii2-nyx-sendgrid
1. Go to this page and download the library: Download nyx-solutions/yii2-nyx-sendgrid library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nyx-solutions / yii2-nyx-sendgrid example snippets
'mailer' => [
'class' => \nyx\mail\sendgrid\Mailer::class,
'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' => false,
'apiKey' => '[YOUR_SENDGRID_API_KEY]',
],
$user = \common\models\User::find()->select(['id', 'username', 'email'])->where(['id' => 1])->one();
$mailer = Yii::$app->mailer;
$message = $mailer->compose()
->setTo([$user->email => $user->username]) // or just $user->email
->setFrom(['[email protected] ' => 'Alerts'])
->setReplyTo('[email protected] ')
->setSubject('Hey -username-, Read This Email')
->setHtmlBody('Dear -username-,<br><br>My HTML message here')
->setTextBody('Dear -username-,\n\nMy Text message here')
//->setTemplateId('1234')
//->addSection('%section1%', 'This is my section1')
//->addHeader('X-Track-UserType', 'admin')
//->addHeader('X-Track-UID', Yii::$app->user->id)
//->addCategory('tests')
//->addCustomArg('test_arg', 'my custom arg')
//->setSendAt(time() + (5 * 60))
//->setBatchId(Yii::$app->mailer->createBatchId())
//->setIpPoolName('7')
//->attach(Yii::getAlias('@webroot/files/attachment.pdf'))
->addSubstitution('-username-', $user->username)
->send();
if ($message === true) {
echo 'Success!';
echo '<pre>' . print_r($mailer->getRawResponses(), true) . '</pre>';
} else {
echo 'Error!<br>';
echo '<pre>' . print_r($mailer->getErrors(), true) . '</pre>';
}
$mailer = Yii::$app->mailer;
//$batchId = Yii::$app->mailer->createBatchId();
//$sendTime = time() + (5 * 60); // 5 minutes from now
foreach (User::find()->select(['id', 'username', 'email'])->batch(500) as $users)
{
$message = $mailer->compose()
->setFrom(['[email protected] ' => 'Alerts'])
->setReplyTo('[email protected] ')
->setSubject('Hey -username-, Read This Email')
->setHtmlBody('Dear -username-,<br><br>My HTML message here')
->setTextBody('Dear -username-,\n\nMy Text message here');
//->setTemplateId('1234')
//->addSection('%section1%', 'This is my section1')
//->addHeader('X-Track-UserType', 'admin')
//->addHeader('X-Track-UID', Yii::$app->user->id)
//->addCategory('tests')
//->addCustomArg('test_arg', 'my custom arg')
//->setSendAt($sendTime)
//->setBatchId($batchId)
//->setIpPoolName('7')
//->attach(Yii::getAlias('@webroot/files/attachment.pdf'));
foreach ( $users as $user )
{
// A Personalization Object Helper would be nice here...
$personalization = [
'to' => [$user->email => $user->username], // or just `[email protected] `
//'cc' => '[email protected] ',
//'bcc' => '[email protected] ',
//'subject' => 'Hey -username-, Custom message for you!',
//'headers' => [
// 'X-Track-RecipId' => $user->id,
//],
'substitutions' => [
'-username-' => $user->username,
],
//'custom_args' => [
// 'user_id' => $user->id,
// 'type' => 'marketing',
//],
//'send_at' => $sendTime,
];
$message->addPersonalization($personalization);
}
$result = $message->send();
}
if ($result === true) {
echo 'Success!';
echo '<pre>' . print_r($mailer->getRawResponses(), true) . '</pre>';
} else {
echo 'Error!<br>';
echo '<pre>' . print_r($mailer->getErrors(), true) . '</pre>';
}