'cakephp 3.2 email with sendgrid issue

I'm having a hard time trying to figure out how to send mails with sendgrid.

this is the code i currently have:

employees controller:

function _sendEmail($id) {
    $email = new Email();
        try {
            $email->from(['[email protected]' => 'Me'])
                ->profile('SendGrid')
                ->to([$id['email'] => $id['full_name']])
                ->subject("TEST SUBJECT")
                ->emailFormat("both")
                ->template('default')
                ->send('My message');
            $this->Flash->success("Message sent.");
        } catch (Exception $ex) {
            echo 'Exception : ', $ex->getMessage(), "\n";
        }                              
        return $this->redirect(['action' => 'index']);
}

I'm working with this plugin I found a few days ago; https://github.com/Iandenh/cakephp-sendgrid... I configured everything as stated in the docs but when I want to send the mail, nothing happens, the function flashes the success message and makes the redirection, but no email is sent.

This is the email transport in the app.php

'EmailTransport' => [
    'SendGridEmail' => [
        'className' => 'SendGridEmail.SendGrid',
        'api_key' => 'API_KEY'
    ],

and the delivery profile

'Email' => [
    'SendGrid' => [
        'transport' => 'SendGridEmail',
        'template' => 'default',
        'layout' => 'default',
        'emailFormat' => 'both',
        'from' => ['[email protected]' => 'Me'],
        'sender' => ['[email protected]' => 'Me'],
    ]
]

I'd really appreciate if someone can point me out any mistake or a possible solution for my problem.



Solution 1:[1]

Hi there this might be too late but in case if anyone facing the same issue.

This is working for me

So the first mistake you did is you are using the Email class, with sendgrid you should be using sendgrid Mail method now

I assume you have already installed this package if not go ahead and add it to the composer.json file and update composer

 "sendgrid/sendgrid": "~7",

After that you can use sendgrid class for sending emails like shown in example below

    protected function sendEmail($to, $subject, $content)
{
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom(Configure::consume('App.from_email'), Configure::consume('Theme.title'));
    $email->setSubject($subject);
    $email->addTo($to);
    $email->addContent("text/html", 'Your email body');
    $sendgrid = new \SendGrid(Configure::consume('App.sendgrid_api_key'));
    try {
        $response = $sendgrid->send($email);
        print $response->statusCode() . "\n";
        print_r($response->headers());
        print $response->body() . "\n";
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."\n";
    }
}

You need to replace sendgrid_api_key with your api key. I am using it from my configuration file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shahzaib