Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send email via sendgrid curl using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.33k
    Comment on it

    Hello Reader's if you are looking to send emails via curl using PHP then this blog is helpful to you.
    Curl is a tool which sent the data in forms to any other server with multiple data. So lets get started working on sending email with it.

    Step 1:-  Create a php code where you need to defined the user email address, email subject and its  matter. Assign them in individual variables separately. This code will be written inside your controller and it will go like this:-

    $subject = "This is an email test blog on findnerd";
    $mail_message= "Hello this is blog on email via curl in findnerd";
    $mail_message.='<br>Thanks, <br>Abhishek Tiwari';
    $from = "abc@yourwebiste.com"
    $to = "xyz@website.com"
    $response = $this->mailmodel->sendEmail($to, $from, $subject, $mail_message); 

    In the code above you can set the target in var $to which is default as "xyz@website.com". Also you can type matter which you want to send in the email in var $subject.

    Step2. Now create a new model and name it to mailmodel.php and its code will go like this:-

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Smsmodel extends CI_Model {
    			public function sendEmail($to, $from, $subject, $message)
    		{  				
    				$params = array(
    				    'api_user'  => 'username',
    				    'api_key'   => 'your_api_key',
    				    'to'        => $to,
    				    'from'      => $from,
    				    'subject'   => $subject,
    				    'html'      => $message,				    
    				  );
    
    				$request = 'https://api.sendgrid.com/api/mail.send.json';
    				$session = curl_init($request);
    				curl_setopt ($session, CURLOPT_POST, true);
    				curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    				curl_setopt($session, CURLOPT_HEADER, false);
    				curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    				$response = curl_exec($session);
    				curl_close($session);
    				$msg = json_decode($response);
    				return $msg->message;
    		}
    ?>

    Now the function sendEmail() wil call by your controller this action in model will loads. And "$session = curl_init($request);" will load the curl. You must remember to check weather curl is enable or not in your server.
    Now put the sendgrid credential to this curl params. And when the curl finished the server of sendgrid will send you a response in json format. This is the resonse code and status of your email request 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: