Code Monkey home page Code Monkey logo

sendgrid-objc's Introduction

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];

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

addSubstitution

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

NSMutableDictionary *subs = [header getSubstitutions];

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];

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];

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];

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.

sendgrid-objc's People

Contributors

heitortsergent avatar iancarv avatar exoram avatar natashatherobot avatar nquinlan avatar amishralhan avatar brandonmwest avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.