All Projects → sendgrid → Sendgrid Objc

sendgrid / Sendgrid Objc

Licence: mit
SendGrid Objective-C helper library

SendGrid-Objc

  • We will be dropping support for this lib due possible security concerns of applications being decompiled. Please only use in backend code.

This library allows you to quickly and easily send emails through SendGrid using Objective-C.

Important: This library requires AFNetworking 2.0 or higher.

SendGrid *sendgrid = [SendGrid apiUser:@"username" apiKey:@"password"];   

SendGridEmail *email = [[SendGridEmail alloc] init];
email.to = @"[email protected]";
email.from = @"[email protected]";
email.subject = @"Hello World";   
email.html = @"<h1>My first email through SendGrid</h1>";
email.text = @"My first email through SendGrid";

[sendgrid sendWithWeb:email];

Installation

Choose your installation method - CocoaPods (recommended) or source.

Installation via CocoaPods (Recommended Method)

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like SendGrid and its dependencies in your projects. Simply add the lines below to your existing Podfile or make a new 'Podfile' that contain the lines below.

Podfile

platform :ios, '7.0'
pod 'SendGrid', '~>  0.2.6'

Run the following in the command line:

pod install

Be sure to open up the .xcworkspace file now instead of the .xcodeproj file.

Then import the library - in the file appropriate to your project.

import <SendGrid/SendGrid.h>

Alternative installation

Install via Source

1. Clone this repository.
2. Copy SendGrid.h and .m files to your project.
3. Clone the [SMTPAPI repository](https://github.com/heitortsergent/smtpapi-ios).
4. Copy SMTPAPI.h and .m files to your project.
5. Import SendGrid.h in the file appropriate to your project, and AFNetworking in your project.

Demo App

There's a demo app on the Github repository, inside the folder "Demo". It's a simple application that lets you send an email, with an Image Picker if you want to send a picture attachment with it.

To run the demo, clone the repository (it's not available when you install via CocoaPods), open the terminal and go to the demo folder. Then do:

pod install
open Demo.xcworkspace

Usage

To begin using this library, import the library into your project.

#import <SendGrid/SendGrid.h>
#import <SendGrid/SendGridEmail.h>

Create a new SendGrid object with your SendGrid credentials.

SendGrid *sendgrid = [SendGrid apiUser:@"username" apiKey:@"password"];

Create a new SendGridEmail object, and customize the parameters of your message.

SendGridEmail *email = [[SendGridEmail alloc] init];
email.to = @"[email protected]";
email.from = @"[email protected]";
email.subject = @"Hello World";   
email.html = @"<h1>My first email through SendGrid</h1>";
email.text = @"My first email through SendGrid";

Send it.

[sendgrid sendWithWeb:email];

addTo

You can add one or multiple TO addresses using addTo.

[email addTo:@"[email protected]"];
[email addTo:@"[email protected]"];

setTos

[email setTos:@[@"[email protected]", @"[email protected]"]];

addBcc

[email addBcc:@"[email protected]"];

setFrom

[email setFrom:@"[email protected]"];

setFromName

[email setFromName:@"Other Dude"];

setReplyTo

[email setReplyTo:@"[email protected]"];

setSubject

[email setSubject:@"Hello World"];

setText

[email setText:@"This is some text of the email."];

setHtml

[email setHtml:@"<h1>My first email through SendGrid"];

addSubstitution

[email addSubstitution:@"key" val:@"value"];

addUniqueArg

[email addUniqueArg:@"key" val:@"value"];

addCategory

[email addCategory:@"category"];

addSection

[email addSection:@"key" val:@"value"];

addFilter

[email addFilter:@"filter" setting:@"setting" val:@"value"];
[email addFilter:@"filter" settings:@"setting" val:1];

Adding an image attachment

attachImage

You can add an image attachment to your email message. The method accepts a UIImage.

[email attachImage:self.photo];

Displaying attached image inline

email.inlinePhoto = true;
email.html = @"<img src =\"cid:image0.png\"><h1>hello world</h1>";

attachFile

UIImage *sendgridLogo = [UIImage imageNamed:@"sendgrid_logo.png"];

SendGridEmailAttachment* someImageAttachment = [[SendGridEmailAttachment alloc] init];
someImageAttachment.attachmentData = UIImagePNGRepresentation(sendgridLogo);
someImageAttachment.mimeType = @"image/png";
someImageAttachment.fileName = @"sendgrid_logo";
someImageAttachment.extension = @"png";

[email attachFile:someImageAttachment];

X-SMTPAPI

This library uses the SMTPAPI object which is found in SMTPAPI-iOS.

Substitutions

addSubstitution

[header addSubstitution:@"key" val:@"value"];

NSMutableDictionary *subs = [header getSubstitutions];

Unique Arguments

addUniqueArg

[header addUniqueArg:@"key" val:@"value"];
[header addUniqueArg:@"key2" val:@"value2"];

NSMutableDictionary *args = [header getUniqueArgs];

setUniqueArgs

NSMutableDictionary *uniqueArgs = [[NSMutableDictionary alloc] init];
[uniqueArgs setObject:@"value" forKey:@"unique"];
[header setUniqueArgs:uniqueArgs];

NSMutableDictionary *args = [header getUniqueArgs];

Categories

addCategory

[header addCategory:@"category1"];
[header addCategory:@"category2"];

NSMutableArray *cats = [header getCategories];

addCategories

[header addCategories:@[@"category1", @"category2"]];

NSMutableArray *cats = [header getCategories];

setCategories

[header setCategories:@[@"category1", @"category2"]];

NSMutableArray *cats = [header getCategories];

Sections

addSection

[header addSection:@"key" val:@"section"];

NSMutableDictionary *sections = [header getSections];

setSections

NSMutableDictionary *newSec = [[NSMutableDictionary alloc] init];
[newSec setObject:@"value" forKey:@"-section-"];
[header setSections:newSec];

NSMutableDictionary *sections = [header getSections];

Filters

addFilter

[header addFilter:@"filter" setting:@"setting" val:@"value"];
[header addFilter:@"filter" settings:@"setting" val:1];

NSMutableDictionary *filters = [header getFilters];

Get Headers

[header configureHeader];
NSString *headers = header.encodedHeader;

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].