0
votes

I have integrated sendgrid for send mail. I also want to track whether a user has opened the mail and click the link inside the mail or not.
that's why I used sendgrid.
using it I can send mail, but can't track mail states(mail is opened or not, the link is clicked or not).
I tried the below code for sending mail.

 function sendMail($toMails, $body, $subject, $ccMails = array(), $bccMails = array()) {
        $ci = &get_instance();
        if (empty($toName)) {
            $toName = $toMails;
        }
        $sendMail = $ci->config->item('sendMail');
        $email = new \SendGrid\Mail\Mail();
        $email->setFrom($ci->config->item('from'), "From User name");
        $email->setSubject($subject);

        $email->addTos($toMails); //for multiple user pass array with emails and names
        $email->addCcs($ccMails);
        $email->addBccs($bccMails);
        $email->addContent("text/html", $body);
        $email->setFooter(false, "", "<strong>If you don't want to receive this type of email in the future, please <a href='http://w3schools.com'>Unsubscribe</a>.</strong>");
        //$email->setSpamCheck(true, 1, "http://localhost:8081/");
// Tracking Settings
        $email->setClickTracking(true, true);
        //$email->setOpenTracking(true, true);
        $sendgrid = new \SendGrid($ci->config->item('key'));
        try {
            $response = $sendgrid->send($email);
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "\n";
        }
        if ($sendMail) :
            if (!$response->statusCode()) :
                _pre($response->headers());
                return false;
            else :
                return true;
            endif;
        endif;

    }

which is working fine, except it is going in the spam.

now below code, I am using to get details as per email id.

 $sendgrid = new \SendGrid($this->config->item('key'));
        $query_params = json_decode('{"start_date": "2019-10-07","end_date": "2019-10-07","to_email": "[email protected]","subject":"This is a subject test"}');
        $response = $sendgrid->client->stats()->get(null, $query_params);
        _pre($response->body());
        exit;

above code only gives me date wise data, but I also want email id wise.
but in spite of adding a parameter for that, still, I am not getting desired output.

https://sendgrid.com/docs/for-developers/sending-email/getting-started-email-activity-api/#filter-by-recipient-email
I have used the above demo, in that demo, they have used curl but I am using CodeIgniter's way. I am not sure about sendgrid version that's why I added both version tag, I used API one.

anyone having a proper solution regarding it?

1

1 Answers

0
votes

I have implemented webhooks to archive my desire output.
for that need to follow steps as per documentation shows

after that need to create a page from where we can get mails status. on executing that page it returns data as per activity.

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');

$data   = file_get_contents("php://input");
$events = json_encode($data, true);

$requestData = array('response' => $events);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "<url which we earlier set for webhook as per documentation>");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);

?>

i have used Curl for getting desire output.