Configuring SendGrid For SMTP Integration In Laravel and CodeIgniter

In this tutorial,i will show you how to integrate sendgrid using laravel and codeigniter.sendgrid is a popular SMTP email provider.https://sendgrid.com/ this is the official site.this provider freely give 100 email perday.


Configuring SendGrid for SMTP integration in both Laravel and CodeIgniter frameworks involves similar steps. Here's a conclusion for setting up SendGrid SMTP integration in both frameworks



Begin by signing up for a SendGrid account. During the signup process, ensure to enable two-factor authentication (2FA). This step is crucial, as SendGrid mandates 2FA for all accounts, and you cannot disable it.

Once signed up, configure 2FA for your account. This additional security measure helps protect your SendGrid account from unauthorized access.


Settings Up SendGrid Account:

After logging in, generate an API key from the SendGrid dashboard. This API key will be necessary for integrating SendGrid with your Laravel application for sending emails.


Step 1 : Api Keys




Step 2 : Create Api Key





How to verify sender id:

Once logged in, navigate to the "Sender Authentication" section. This section may be found under the "Settings" or "Account Settings" menu, depending on the SendGrid dashboard's layout.

SendGrid offers several methods for sender authentication, including Single Sender Verification, Domain Authentication, and Automated Security. Choose the appropriate method based on your requirements.

If you're using Single Sender Verification, enter the sender email address you wish to verify.


Step 1 




Step 2 


Step 3 

 



After completing the verification process, your sender identity will be verified, and you'll receive a confirmation within the SendGrid dashboard.

Now i will show you how to integrate sendgrid using laravel  just add the code in .env file.

Laravel


.env

    MAIL_MAILER=smtp
    # MAIL_DRIVER=smtp # for below laravel 7
    MAIL_HOST=smtp.sendgrid.net
    MAIL_PORT=587
    MAIL_USERNAME=apikey
    MAIL_PASSWORD=api_key
    MAIL_ENCRYPTION=tls
    MAIL_FROM_NAME="Manna Web"
    MAIL_FROM_ADDRESS=from@email.com

You can now send a test email to verify that everything is set up correctly. You can use Laravel's built-in Mail facade to send emails. For example:


<?php

    use Illuminate\Support\Facades\Mail;
    use App\Mail\DemoEmail;

    Route::get('/send-email', function () {
    $demo_email = new DemoEmail();
    Mail::to('recipient@example.com')->send($demo_email);

    return 'Email sent successfully!';
    });

That's it! Your Laravel application is now configured to send emails using SendGrid via SMTP.

CODEIGNITER

Open application/config/email.php in your CodeIgniter project and configure the email settings to use SendGrid SMTP. Set the protocol, smtp_host, smtp_user, smtp_pass, smtp_port, and smtp_crypto to the appropriate values for SendGrid.


  <?php
   /* application/config/email.php */
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        $config['protocol'] = 'smtp';
        $config['smtp_port']    = '587';
        $config['smtp_host']    = 'smtp.sendgrid.net';
        $config['smtp_user']    = 'yourusername';
        $config['smtp_pass']    = 'yourpassword';



In your controller or wherever you want to send emails, load the CodeIgniter email library and use it to send emails. Here's a basic example:

</php

    $this->load->library('email');
    $this->email->from('your@example.com', 'Your Name');
    $this->email->to('recipient@example.com');
    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');

    if ($this->email->send()) {
        echo 'Email sent successfully!';
    } else {
        echo 'Email sending failed.';
    }



By following these steps, you should be able to successfully configure SendGrid for SMTP integration in both Laravel and CodeIgniter frameworks, allowing your applications to send emails through SendGrid's infrastructure.
Previous Post Next Post

Contact Form