0
votes

What i need: I need to send a email from Codeigniter with Sendgrid API key.

I have showed the example below of sending email using SMTP details and username and password in codeigniter.

Example:

Create a file with name "email.php" in application/config folder and paste the below code in it.

/* application/config/email.php */

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
|--------------------------------------------------------------------------
| SendGrid Setup
|--------------------------------------------------------------------------
|
| All we have to do is configure CodeIgniter to send using the SendGrid
| SMTP relay:
*/
$config['protocol'] = 'smtp';
$config['smtp_port']    = '587';
$config['smtp_host']    = 'smtp.sendgrid.net';
$config['smtp_user']    = 'yourusername';
$config['smtp_pass']    = 'yourpassword';
?>

In Controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->library('email');

        $this->email->from('[email protected]', 'John');
        $this->email->to('[email protected]');
        $this->email->subject('Test Email using SendGrid');
        $this->email->message('This email was delivered by your friends at SendGrid');

        $this->email->send();

        echo $this->email->print_debugger();

        $this->load->view('welcome_message');
    }
}
?>

But i need to send mail through the API key. Is there any possibilities to do.

Thanks

1
@MohammedShafeek ya i try it directly in PHP its working fine. But i didn't know how to setup in codeigniter. If you know, Please help me to solve this situation. - Arun

1 Answers

0
votes

Download the PHP sendgrid pach from HERE

Place it inside required folder inside root.

N:B If you are using composer for Codeigniter you can add like below and you can update dependancy using composer update and it will download the pack with your vender folder.

{
  "require": {
    "sendgrid/sendgrid": "~7"
  }
}

Once done go inside vendor/sendgrid-php folder

do composer install inside that folder to install dependency for current library.

You can use library in your controller like as follows.

    require FCPATH .'vendor/sendgrid-php/sendgrid-php.php';

    $email = new \SendGrid\Mail\Mail();
    $email->setFrom("[email protected]", "Example User");
    $email->setSubject("Sending with SendGrid is Fun");
    $email->addTo("[email protected]", "Example User");
    $email->addContent(
        "text/plain", "and easy to do anywhere, even with PHP"
    );
    $email->addContent(
        "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
    );
    $sendgrid = new \SendGrid(getenv('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 will get more library documentation HERE