Sending 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.



Author
Expert advice and insight about all things email including best practices tips, examples, and advice for marketers, developers, and everyone in between.