Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP class to send email via SendGrid service

I'm trying out integration of the SendGrid transactional email service with PHP scripts on my site. For my purpose all I need it to do is to be able to send a single text-based email from the 'Contact' page on my site. And that is it!

So I found this PHP library which unfortunately has too much "stuff" in it. I'd hate to upload 1.7 MB of PHP code to my server without knowing what's going on there.

So I'm curious, is there a simple PHP implementation, idk, using sockets to connect to SendGrid servers to do the following?

  1. Send a single plain-text email to a short list of recipients.

  2. Get a result: success/failure, etc.

like image 988
c00000fd Avatar asked Apr 23 '16 22:04

c00000fd


2 Answers

i used PHPmailer and it works like a charm

    $mail=new PHPMailer();
    $mail->IsSMTP();    
    $mail->Port = 587;
    $mail->SMTPAuth = true;               
    //sendgrid
    $mail->Username="apikey";
    $mail->Password = $api;   //api key from sendgrid
    $mail->Host="smtp.sendgrid.net";
    $mail->SMTPSecure = 'tls';   
    $mail->From = $from;
    $mail->FromName = 'From name';
    $mail->AddAddress($to);  // Add a recipient
    $mail->MsgHTML($sbody);
    $mail->isHTML(true);
    $mail->Body    = $sbody;
    $mail->Subject = $subject;
    if(!$mail->Send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
  }

    $mail->ClearAddresses();
    $mail->ClearAttachments();
    
like image 50
bob_1982 Avatar answered Sep 22 '22 01:09

bob_1982


It is not exactly the answer to my original question, but it is the answer to the question, "Simple PHP class to send email"

I spent over a day trying to make SendGrid PHP samples work for my server. I might be an old school, but I wasn't using Composer for my existing code, and SendGrid examples would not run without it. On top of that, I didn't want to adjust my existing PHP code-base to work with their classes, or go into debugging theirs to extract the working parts. (I had enough headache having been dumped by Mandrill to add more to it.)

I then posted a question on the SendGrid community forum... and received nothing.

I submitted a ticket to their tech support... and received nothing. (To this day.)

So my solution was to keep looking. That's when I found this review of transactional email services, and first tried Amazon SES (which was a big pain in the a%# to set up, so I didn't go with it. Here's a nice YouTube video that will show how complicated just a setup is.)

I then tried SendInBlue and totally liked the ease with which it worked. Their PHP class to send transactional emails is just one .php file, 22 KB in size. No composer, none of other dependencies. It worked without giving me any headache. The deliverability was great as well. I was able to set up an account that ran out-of-the-box within 15 minutes (after all necessary verifications and such.) There were some restrictions on how many emails one can send from a new account, but after you contact their tech support (or establish yourself, I guess) they will take some of those out. So my test email was able to reach Hotmail, Outlook, GMail servers without a problem. It got straight into Inbox. I didn't even have to set up SPF and DKIM records. By default emails you sent use SendinBlue signature. Later I set up my own SPF and DKIM records after I contacted their tech support and received a prompt reply with instructions.

So I'm really impressed with SendinBlue for my needs (i.e. small number of transactional emails.) Even their free account seems to work really nice, and then for $8/mo. you can lift some of their sending quotas.

SendinBlue is a French company, so it may not be affected by some of the legal/political BS that's happening in US now. They are also not as big as SendGrid or Mandrill/Mailchimp so they haven't grown a "big head" yet like those two. So, SendinBlue, let's keep it this way!

Disclaimer: I am not affiliated with any of the services I mentioned above.

like image 21
c00000fd Avatar answered Sep 22 '22 01:09

c00000fd