Home » Php » php – Sendgrid – SMTP or CURL?

php – Sendgrid – SMTP or CURL?

Posted by: admin July 12, 2020 Leave a comment


We are setting up a bulk mailing system using sendgrid as our core.

We are managing the lists ourselves and sendgrid is simply our transport and are using code igniter to build the system.

We are wondering what you would recommend we use, sendgrid as an smtp server, or use it’s curl API.

We are sending emails out to x00,000 people every day, the emails all have the same content.

We have found SendGrid’s integration documentation for code igniter which only has smtp examples, so possibly that is the way to go?

The other part of the question is, if we were to go with the SMTP api, how does code igniters bcc_batch_mode work?

How to&Answers:

Reading the documentation at http://sendgrid.com/documentation/map/version/v2#api
there is little to choose between them. All functionality is available either way. For PHP, I recommend smtp server with their SMTP API to take advantage of existing libraries that correctly format, mime encode, and send the email. Notably, example PHP code is given only for the SMTP API.

Edited for additions to question

The sendgrid documentation say to encode multiple recipients with their API in a custom mail header — X-SMTPAPI –not using CC and BCC. See http://sendgrid.com/documentation/display/api/SMTPDevelopersGuide

http://sendgrid.com/documentation/display/api/SMTP best practices section for issue with multiple recipients in the regular mail headers.


I currently work at SendGrid. Our web API actually works faster than SMTP, as you only need to make a single cURL request to us to send a message, whereas with SMTP there’s a lot of back-and-forth TCP chatter for connection, HELO, and such.

We recently published a new PHP library, you can find it on our github account: http://github.com/sendgrid/

Don’t hesitate to contact us if you have questions.


Ok, so simply as a reference for anyone I am going to paste my support chat. Note that this chat covers a wide range of things so could be useful to have it stored here

me: Sending to multiple recipients using code igniter?
Hi there, I am a php developer using code igniter, we are using sendgrid as our email sending platform,

Support: Hello.

me: Hi,

Support: Let me see if I can find a reference in our docs.

me: I believe you are going to reference: http://bit.ly/jL1Pde

Support: That was the one I was looking for.

me: Yes, I have seen that, But I also saw http://bit.ly/jvowuk which says you should use the X-SMTPAPI header, so I am a bit confused as to which I should use?

Support: Use the Codeigniter example from the previous link. You could use X-SMTPAPI, but is not needed in this case. The reason is for better portability with other languages.

me: Oh I see, Thank you very much, I have one other query.

Support: Ok.

me: When sending emails is there any header we can send to associate extra information with an email (for example a id from our system) to help with the lookup?

Support: You can set a custom category if needed. setCategory(cat) Sets a category for an e-mail to be logged as. You can use any category name you like. This is from http://bit.ly/iYjq2G

me: Oh I see, thank you, and sorry to be a pain but I have one more question,

Support: Ok.

me: We wish to provide our users with the ability to unsubscribe from receiving emails, All emails send through in a specific batch (e.g. 100,000 emails) will have the exact same content, with the exception that we want to append an unsubscribe message to the bottom, We want to be able to track the unsubscribe back to a specific message sent from our system, so we will want to have the url look something like http://example.com/unsubscribe/1234, 1234 being the unique id for the message sent, does sendgrid provide an easy way to complete this, or must we do an individual request for every message?

Support: Ok, so you can do this using: addFilterSetting(filter, setting, val) Adds/changes a setting for a filter. Settings specified in the header will override configured settings. Here is an example of the parameter being enabled in PHP: $hdr->addFilterSetting(‘subscriptiontrack’, ‘enable’, 1);

me: ok so looking at http://bit.ly/k49a57 it says about your custom name to appear in the link, what exactly does this mean? as we do not wish the user to be sent to sendgrid to unsubscribe.

Support: So, it allows you to make the link say something different other than what we provide such as ‘Remove myself from this company’ for example.

me: Oh I see, So if we wish the url to be something personal we must manage this ourselves, this is fine, I cant remember where, but I noticed somewhere there was something about substitutions for the email content, would this allow us to send through a list of ids and in our email body we could have *example.com/unsubscribe/–email_id–* and have it replace the *–email_id–* with the passed id?

me: Ah, http://bit.ly/jvowuk point 2 is what I was referring to, could I use that?

Support: Yes you could do that.

me: ah, here is a better example, http://bit.ly/lK6ltE

Support: Yes, that shows the e-mail with substitution. So, it can be modified for each.

me: So, I can use *$this->email->_set_header(‘Custom-Header’, ‘value’);* in code igniter to set a custom header, if I was sending out say 3000 emails, I have an array that looks like array(5,6,7,8…) with the ids, 3000 of them How would I send these through? (if it is not a quick answer I can work it out by looking in the SmtpApiHeader class)

Support: The SmtpApiHeader is the best way. Adding them as parameters for the substitution. Each indexed ID.

session disconnected.