SendGrid email integration with PHP
SendGrid email integration with PHP
SendGrid (https://sendgrid.com/) is preferred email solution of lot of small and medium size enterprises. As SendGrid’s website describes
“SendGrid’s cloud-based email infrastructure relieves businesses of the cost and complexity of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time analytics along with flexible APIs that make custom integration a breeze.”
This entry explains how you can easily integrate SendGrid in your PHP project.
Before you start, create a free account with SendGrid as you would need it in the sample code.
Next GIT clone Sendgrid’s PHP library. The command below would clone it in a folder named sendgrid-php.
sudo git clone https://github.com/sendgrid/sendgrid-php.git
If you do not have git installed you can always install it using:
sudo apt-get install git-core
SendGrid’s PHP library requires, autoloading
In order to install autoloading, we first need to install composer in the same directory as sendgrid-php directory.
sudo curl --silent https://getcomposer.org/installer | php
Just in case you do not have curl installed, you can always install it using following command.
sudo apt-get install curl
Now we need to generate autoload.php. It can be generated using following command:
php composer.phar install
SendGrid requires swift mailer that can be installed using following command.
pear channel-discover pear.swiftmailer.org pear install swift/swift
Now we are all set. Let’s write a sample code to send a mail using SendGrid. Create a PHP file and paste the following code in it:
include('/path where we took git clone/sendgrid-php/SendGrid_loader.php'); $sendgrid = new SendGrid('sendgrid username', 'sendgrid password'); // username/pwd can be found in the SendGrid control panel. $mail = new SendGridMail(); $mail->addTo($mail_to)->setFrom($from)->setSubject($subject)->setText($subject)->setHtml($body); $sendgrid->smtp->send($mail);
That’s it!! You are ready to mail the world using SendGrid. Also, we are happy to share that Tudip is recognized as Top PHP Development Companies on Software Development Company.