PHP code example of iyngaran / send-grid-transport

1. Go to this page and download the library: Download iyngaran/send-grid-transport 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/ */

    

iyngaran / send-grid-transport example snippets


// config/autoload/mail.global.php

return [
    'mail' => [
        'sendgrid' => [
            'api_key' => 'YOUR_API_KEY',
        ]
    ],
    'test-email' => [
        'from' => [
            'name' => 'Iyngaran Iyathurai',
            'email' => '[email protected]'
        ],
        'to' => [
            'name' => 'Your name',
            'email' => 'your email address'
        ]
    ]
];

// config/modules.config.php
return [
    'Zend\ServiceManager\Di',
    ....
    'Application',
    'SendGridTransport'
];


namespace SendGridTransport\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use SendGridTransport\Mail\Transport\SendGridTransport;

class SendEmailController extends AbstractActionController
{
    private $sendGridTransport;
    private $config;

    public function __construct(SendGridTransport $sendGridTransport, array $config)
    {
        $this->sendGridTransport = $sendGridTransport;
        $this->config = $config;
    }


    public function indexAction()
    {
        $message = new \SendGridTransport\Mail\Message();
        $body = '<strong>Hello :),</strong><BR/> The SendGridTransport is working now :)';
        $message->setBody($body);
        $message->setFrom(
            new \Zend\Mail\Address(
                $this->config['test-email']['from']['email'],
                $this->config['test-email']['from']['name']
            )
        );
        $message->addTo(
            new \Zend\Mail\Address(
                $this->config['test-email']['to']['email'],
                $this->config['test-email']['to']['name']
            )
        );
        $message->setSubject('Testing SendGridTransport - Iyngaran');
        $message->setBodyText('Hello, the SendGridTransport is working now :)');
        print('<pre>');
        print_r($this->sendGridTransport->send($message));
        print('<pre/>');

        return [];
    }
}