Send Email in iOS With New SendGrid Library

Send Email in iOS With New SendGrid Library

Product, Technical
TwitterFacebookLinkedIn

xcode logoSending email programmatically through Objective-C has not an optimal experience. That’s why I created the SendGrid Objective-C library, so developers can easily send emails from their iOS apps.

Without this SendGrid library, developers have a couple sub-par options for sending email. The first is to implement the MFMailComposeViewController. The drawback here is it presents the user with a mail compose screen and they have to hit send. The other option is to implement an SMTP library such as skpsmtpmessage. However, there is a lot of boilerplate code you need to reuse to send a message. Plus, adding photo attachments and working with them is not a smooth experience.

The Sendgrid Objective-C library addresses these issues and allows app developers to take advantage of our features and practices that allow for detailed analytics and increased deliverability.

Here’s the basic code for how you can send email with iOS:

//create Email Object
sendgrid *msg = [sendgrid user:@"ApiUser" andPass:@"ApiKey"];
    
//set parameters
msg.subject = @"email subject";
msg.tolist = @[@"foo@bar.com", @"foo2@bar.com"];
msg.from = @"originalfoo@bar.com";
msg.text = @"hello world";
msg.html = @"<html><body><h1>hello world</h1></body></html>";
    
//adding unique arguments (Optional)
NSDictionary *uarg = @{@"customerAccountNumber":@"55555",
                           @"activationAttempt": @"1"};
[msg addCustomHeader:uarg withKey:@"unique_args"];
    
//adding categories (Optional)
NSString *replyto = @"billing_notifications";
[msg addCustomHeader:replyto withKey:@"category"];
    
//Image attachment (Optional)
[msg attachImage:self.photo];
    
//Send email through Web API Transport
[msg sendWithWeb];

More detailed instructions are available on GitHub. You can install the library by either cloning the repository or using CocoaPods.


More Posts by Kunal
Meet Kunal, Developer Evangelist at-large for SendGrid and home-grown New Jersey hacker. He previously ran General Machines, which developed Deaftel - a phone service for the deaf that converts voice to text and text back into voice over a phone call. Since March 2013, Kunal has been working as a Developer Evangelist at SendGrid, helping enable developers to get the tools and resources they need to make awesome, creative stuff.
Follow Kunal