e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

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.

Related Posts

Latest Posts

  • All Posts
  • Generative AI
  • manufacturing
  • News
  • Portfolio
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com