Send Email with PHP and SendGrid

0
62

Using PHP with cURL. If you choose not to use SendGrid’s client library you may use PHP’s cURL function to query the web API.

function sendMail($email, $formName, $subject, $msg, $from,$reply_to,$html,$path,$file_name,$file_name2,$file_name3) {
		
//	($email,$formName, $subject, $msg, $from,$reply_to,$html,$path,$file_name,$file_name2,$file_name3)
  
  $mail_catg_type="";
  		
  $pepipost_parameter_array = array();

  $pepipost_parameter_array['to_email'] = $email;
  $pepipost_parameter_array['from_name'] =  $formName;
  $pepipost_parameter_array['from_email'] = $from; 
  $pepipost_parameter_array['to_name'] = '';
  $pepipost_parameter_array['mail_catg_type'] = $mail_catg_type;
  $pepipost_parameter_array['subject'] = $subject; 
  $pepipost_parameter_array['reply_to'] = $from; 
  $pepipost_parameter_array['reply_to_name'] =  $formName;
  $pepipost_parameter_array['message'] = $msg;

  $return_array = array();
  $fileContent = array();


  $pepipost_parameter_array['from_name'] = $formName;

  if(empty($pepipost_parameter_array['from_email'])) {

    $pepipost_parameter_array['from_email'] = 'info@shudhmatrimonial.com';
  }

  $personalizations_array = array();

  $personalizations_array["email"] = $pepipost_parameter_array['to_email'];
  
  if(!empty($pepipost_parameter_array['to_name'])) {
    
    $personalizations_array["name"] = $pepipost_parameter_array['to_name'];
  }

  

 
  $jsn_array = array();

  $json_string = array("category" => $pepipost_parameter_array['mail_catg_type']);

  
  $jsn_array['personalizations'][0]["to"][] = $personalizations_array;
  $jsn_array['personalizations'][0]['subject'] = $pepipost_parameter_array['subject'];

  $jsn_array["from"]["email"] = $pepipost_parameter_array['from_email'];
  $jsn_array["from"]["name"] = $pepipost_parameter_array['from_name'];

  if(!empty($pepipost_parameter_array['reply_to'])) {
    $jsn_array["reply_to"]["email"] = $pepipost_parameter_array['reply_to'];
  }

  if(!empty($pepipost_parameter_array['reply_to_name'])) {
    $jsn_array["reply_to"]["name"] = $pepipost_parameter_array['reply_to_name'];
  }
 
  $jsn_array['content'][] = array("type"=>"text/html", "value"=>$pepipost_parameter_array['message']);

   $curl_json = json_encode($jsn_array, TRUE);
 
   $headers = [];

   $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send",
    CURLOPT_HEADERFUNCTION=>function($curl, $header) use (&$headers) {
    $len = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) < 2) // ignore invalid headers
      return $len;

    $name = strtolower(trim($header[0]));
    if (!array_key_exists($name, $headers))
      $headers[$name] = [trim($header[1])];
    else
      $headers[$name][] = trim($header[1]);

    return $len;
    },
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $curl_json,
    CURLOPT_HTTPHEADER => array(
      "authorization: Bearer api_code_send_grid_pest_hearcodeSG.dfg7uiBvTYOkrlNox8PJsg.hg4RuhFJgVkw9CeQFpKnguSuZQeyhErAjIB3l5pCoYU",
      "content-type: application/json"
    ),
  ));

  $response = curl_exec($curl);
 
  $err = curl_error($curl);
  
  curl_close($curl);
 
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here