Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

SendGrid support - Feature Request

Hello developers!

First, thanks for the great tool.

SendGrid is a (free) SMTP service that has a lot of great features.

For those of us unfortunate to be hosted on a service that does not allow access to external SMTP servers (like Bluehost, Hostgator, etc.), it would be great to be able to use the API from SendGrid to get around that problem.

I have looked into it a bit, and SendGrid provides a nice php API to integrate with.

Converting the existing class.mailer.php to use the SendGrid API instead of PEAR:mail, is a bit out of my league however.  There is a lot of manipulation of headers and other mail items in class.mailer.pho that I do not understand, and not sure how to convert to use SendGrid.  Although I suspect for someone in the know, it is not a big effort.

So, I am throwing it out there as it might be useful for anyone wanted to use a third party SMTP service, on a host that does not directly allows access to the SMTP server from the third party.

Thanks again!
Mark

Comments

  • OK, so I have it mostly working and figured I would share it in the hopes of someone who knows a bit more cleaning things up and adding anything that needs to be added.

    For example, in the below changes, inline attachment/images are not being included.  This should be fixed.  Everything else is working as far as I know.

    All changes need to be made in /include/class.mailer.php

    You can download the SendGrid PHP API from here:https://github.com/sendgrid/sendgrid-php

    I installed it in the /include folder of osTicket.

    Changes to class.mailer.php:

    include_once(INCLUDE_DIR.'class.email.php');
    require_once(INCLUDE_DIR.'html2text.php');
    +require_once(INCLUDE_DIR.'sendgrid-php/sendgrid-php.php'

    -------------------------------------------------------------------------------------------------------------------------------------------

            if (is_object($to) && is_callable(array($to, 'getEmail'))) {
                // Add personal name if available
                if (is_callable(array($to, 'getName'))) {
    + $toName = sprintf('"%s"', $to->getName()->getOriginal());
    + $toEmail = $to->getEmail();
                    $to = sprintf('"%s" <%s>',
                        $to->getName()->getOriginal(), $to->getEmail()
                    );
                }
                else {
    + $toName = "";
    + $toEmail = $to->getEmail();
                    $to = $to->getEmail();
                }
            }


    -----------------------------------------------------------------------------------------------------------------------------------------------------

    Contiued on next post.


  • + // Hard coded.  Should be an option on the mail config.
    + $sendgrid = true;
    + if ($sendgrid) {
    + $ost->logDebug("Sending with sendgrid",$this->getFromAddress());
    +
    + $mail = new SendGrid\Mail();
    +
    + $from = new SendGrid\Email(preg_replace("/(\r\n|\r|\n)/s",'', trim($this->getEmail()->getName())), $this->getEmail()->getEmail());
    + $mail->setFrom($from);
    +
    + $mail->setSubject($subject);
    +
    + if ($isHtml)
    + $content = new SendGrid\Content("text/html", $message);
    + else
    + $content = new SendGrid\Content("text/plain", $message);
    + $mail->addContent($content);
    +
    + $personalization = new SendGrid\Personalization();
    + $to = new SendGrid\Email(preg_replace("/(\r\n|\r|\n)/s",'', trim($toName)), $toEmail);
    + $personalization->addTo($to);
    +
    + foreach ($headers as $key => $value){
    + $ost->logDebug($key,$value);
    + if ($key != "To" && $key != "From" && $key != "Subject" && $key != "Content-Type")
    v $personalization->addHeader($key,$value);
    + }
    + $mail->addPersonalization($personalization);
    +

  • +     if(($attachments=$this->getAttachments())) {
    + $ost->logDebug("Found attachments","");
    + foreach($attachments as $attachment) {
    + if ($attachment['file_id']
    + && ($file=AttachmentFile::lookup($attachment['file_id']))) {
    +
    + $ost->logDebug("Adding attachment",$file->getData());
    +
    + $attachment = new SendGrid\Attachment();
    + $attachment->setContent(base64_encode($file->getData()));
    + $attachment->setType($file->getType());
    + $attachment->setFilename($file->getName());
    + $attachment->setDisposition("attachment");
    + $mail->addAttachment($attachment);
    +
    + $ost->logDebug("Added attachment",$file->getName());
    +
    + }
    + }
    + }
    +
    + $apiKey = "???";
    + $sg = new \SendGrid($apiKey);
    +
    + $response = $sg->client->mail()->send()->post($mail);
    +
    + if ($response->statusCode() == 202) {
    + $ost->logDebug("Sent via sendgrid","");
    + return $messageId;
    + }
    + else {
    + $ost->logError("Sent via sendgrid failed",strval($response->statusCode()));
    + $ost->logError("Sent via sendgrid failed",strval($response->body()));
    + return false;
    + }
    + }
    + else {
            // Cache smtp connections made during this request
            static $smtp_connections = array();
            if(($smtp=$this->getSMTPInfo())) { //Send via SMTP

Sign In or Register to comment.