PHP code example of marketforce-info / yii2-sendgrid

1. Go to this page and download the library: Download marketforce-info/yii2-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/ */

    

marketforce-info / yii2-sendgrid example snippets


    'mailer' => [
        'class' => \MarketforceInfo\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]',
    ],

    $mailer = Yii::$app->mailer;
    $message = $mailer->compose()
        ->setTo('[email protected]')
        ->setFrom(['[email protected]' => 'Alerts'])
        ->setReplyTo('[email protected]')
        ->setSubject('Example Email Subject')
        ->setTextBody('Example email body.')
        ->send();

    $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])
        ->setFrom(['[email protected]' => 'Alerts'])
        ->setReplyTo('[email protected]')
        ->setSubject('Example Email Subject')
        ->setHtmlBody('Dear -username-,<br><br>Example email HTML body.')
        ->setTextBody('Dear -username-,\n\nExample email text body.')
        ->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;

    foreach (User::find()->select(['id', 'username', 'email'])->batch(500) as $users) {

        $message = $mailer->compose()
            ->setFrom(['[email protected]' => 'Alerts'])
            ->setReplyTo('[email protected]')
            ->setSubject('Hey -username-, Example Email Subject')
            ->setHtmlBody('Dear -username-,<br><br>Example email HTML body.')
            ->setTextBody('Dear -username-,\n\nExample email text body.')

        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>';
    }