<?php

if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != "") $firstname=$_REQUEST['firstname'];
if(isset($_REQUEST['phoneno']) && $_REQUEST['phoneno'] != "") $phoneno=$_REQUEST['phoneno'];
if(isset($_REQUEST['emailid']) && $_REQUEST['emailid'] != "") $emailid=$_REQUEST['emailid'];
if(isset($_REQUEST['machine']) && $_REQUEST['machine'] != "") $machine=$_REQUEST['machine'];
if(isset($_REQUEST['cityid']) && $_REQUEST['cityid'] != "") $cityid=$_REQUEST['cityid'];
if(isset($_REQUEST['stateid']) && $_REQUEST['stateid'] != "") $state=$_REQUEST['stateid'];

$msg="Name:".$firstname."\nPhone No :".$phoneno."<br>Email id: ".$emailid."<br>Machine:".$machine."<br>City:".$cityid."<br>State:".$state;

SendGridMail('shankar.surya@indiamart.com',$msg);
SendGridMail('j250raj@caterpillar.onmicrosoft.com',$msg);
SendGridMail('Krishnendu.Chakraborty@gainwellindia.com',$msg);
SendGridMail('ishan.nagpal@gainwellindia.com',$msg);
SendGridMail('marketing@gainwellindia.com',$msg);
SendGridMail('sales@gmmcoindia.com',$msg);
SendGridMail('Malhotra_Akshay@cat.com',$msg);
SendGridMail('Vinisha.Mallick@cat.com',$msg);
SendGridMail('akshat.ghiya@indiamart.com',$msg);
SendGridMail('rudranil.dutta@indiamart.com',$msg);
SendGridMail('nonis.jhamb@indiamart.com',$msg);
//SendGridMail('ritika.garg@indiamart.com',$msg);

header( "refresh:5;url=http://catmachine.co.in/" );
function sendgridmail($Receiver_email,$mail_body)
    {
        $category = "CAT_Leads";
        $Subject='Lead Generated from IndiaMART for Ad Campaign';
        $key="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";
        $sent_date=date('d-m-Y', time());
        $umailid = "CAT-$sent_date-".rand(10, 99);  


        $from_email = 'shankar.surya@indiamart.com';
        $fromName="IndiaMART";
        $umail='CAT';
        if($key != ""){

                $categories_key_array = Array(
                $category
                );

                //var_dump($unique_args);
                $indiamart_email = $from_email;
                $reply_to = $replyTo;
                $from_name=$fromName;
                $toarr = explode("," , $Receiver_email);  
                if(array_key_exists(1,$toarr)){
                $to_key_array = Array(0 => Array("email" => $toarr[0]),1 => Array("email" => $toarr[1]));
                }else{
                $to_key_array = Array(0 => Array("email" => $toarr[0]));
                }

                $reply_to_key_array = Array("email" => $reply_to); 

                $request_body = Array(
                "personalizations" => Array
                (
                0 => Array
                (
                "to" => $to_key_array,
                "subject" => $Subject,
                )
                ),
                "from" => Array
                (
                "email" => $indiamart_email,
                "name" => $from_name
                ),
                "content" => Array
                (
                0 => Array
                (
                "type" => "text/html",
                "value" => $mail_body
                )
                ),

                "categories" => $categories_key_array,
                );



                $curl = curl_init();
                // var_dump(json_encode($request_body));
                curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 5,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($request_body),
                CURLOPT_HTTPHEADER => array(
                "authorization: Bearer $key",
                "content-type: application/json"
                ),
                )
                );
                //var_dump($request_body);
                $response = curl_exec($curl);
                //var_dump($response);
                //echo "\n";
                $err = curl_error($curl);
                $errno = curl_errno($curl);
                $status = curl_getinfo($curl);
                // var_dump($err);
                curl_close($curl);

        }
}
?>
<div style="padding:5px; margin: 0 auto; text-align: center; font-family:'open sans', arial; font-size: 30px;"><h1>Thank You!</h1></div>

<div style="font-family:'open sans', arial; font-size:16px; text-align: center; line-height: 25px; max-width: 100%; width:600px; padding: 10px; margin: 0 auto;">We appreciate your interest in <strong>Cat® machines</strong>. <br>
   Our representative from the local dealer will get in touch with you soon to provide more information about the equipment you are interested in and any relevant offers that are available.</div>
