Git Product home page Git Product logo

sendgrid-apex's Introduction

sendgrid-apex

This Apex Toolkit allows you to quickly and easily send emails through SendGrid using Salesforce Apex.

SendGrid sendgrid = new SendGrid('username', 'password');

SendGrid.email email = new SendGrid.Email();
email.addTo('[email protected]');
email.setFrom('[email protected]');
email.setSubject('Subject goes here');
email.setText('Hello World!');

String response = sendgrid.send(email);

Installation

To start using the SendGrid Apex Toolkit in your Salesforce Org, install the unmanaged package of the library with the following URL:

1.0: https://login.salesforce.com/packaging/installPackage.apexp?p0=04tF0000000SwjP

Click Continue -> Next -> Next -> Install.

Usage

To begin using this library, initialize the SendGrid object with your SendGrid credentials.

SendGrid sendgrid = new SendGrid('username', 'password');

Add your message details.

SendGrid.email email = new SendGrid.Email();
email.addTo('[email protected]');
email.setFrom('[email protected]');
email.setSubject('Subject goes here');
email.setText('Hello World!');
email.setHtml('<strong>Hello World!</strong>');

Send it.

String response = sendgrid.send(email);

addTo

SendGrid.email email = new SendGrid.Email();
email.addTo('[email protected]');
email.addTo('[email protected]');

setTos

SendGrid.email email = new SendGrid.Email();
List<String> tos = new List<String> { '[email protected]' };
email.setTos(tos);

setFrom

SendGrid.email email = new SendGrid.Email();
email.setFrom('[email protected]');

setFromName

SendGrid.email email = new SendGrid.Email();
email.setFromName('Example Lady');

setReplyTo

SendGrid.email email = new SendGrid.Email();
email.setReplyTo('[email protected]');

Bcc

Use multiple addTos as a superior alternative to setBcc.

SendGrid.email email = new SendGrid.Email();
email.addTo('[email protected]');
email.addTo('[email protected]');
email.addTo('[email protected]');
...

But if you do still have a need for Bcc you can do the following.

SendGrid.email email = new SendGrid.Email();
email.addBcc('[email protected]');

setSubject

SendGrid.email email = new SendGrid.Email();
email.setSubject('This is a subject');

setText

SendGrid.email email = new SendGrid.Email();
email.setText('This is some text');

setHtml

SendGrid.email email = new SendGrid.Email();
email.setHtml('<h1>This is an html email</h1>');

addSubstitution

SendGrid.email email = new SendGrid.Email();
List<String> vals = new List<String> { 'val' };
email.addSubstitution('sub', vals);

addUniqueArg

SendGrid.email email = new SendGrid.Email();
email.addUniqueArg('add_unique_argument_key', 'add_unique_argument_value');

addCategory

SendGrid.email email = new SendGrid.Email();
email.addCategory('Category 1');
email.addCategory('Category 2');

addSection

SendGrid.email email = new SendGrid.Email();
email.addSection('set_section_key', 'set_section_value');

addFilter

SendGrid.email email = new SendGrid.Email();
email.addFtiler('footer', 'text/html', '<strong>boo</strong>');

addHeader

You can add standard email message headers as necessary.

SendGrid.email email = new SendGrid.Email();
email.addTo('[email protected]');
...
email.addHeader('X-Sent-Using', 'SendGrid-API');
email.addHeader('X-Transport', 'web');

addAttachmentStream

SendGrid.email email = new SendGrid.Email();

// as a string
email.addAttachmentStream('text.txt', 'somerandomcontentyouwant');

// as a blob
String text = 'This is an attachment.';
Blob as_blob = Blob.valueof(text);
email.addAttachmentStream('text.txt', as_blob);

Development

Getting your development environment setup takes some careful steps. So, we have explained the process here to save you some time:

Setup Salesforce.com

Create a salesforce.com developer account.

You'll receive an email from Salesforce with a link to confirm. Click it.

On the next screen, set your password.

You now have a developer account.

Generate your security token. We will need this later. While logged into your developer account, click "Your Name > My Settings" at the top right portion of your developer account dashboard.

Then on the left side of the screen click "Personal > Reset My Security Token". Click the "Reset Security Token" button. Salesforce emails you a security token. You will need this later.

Setup local environment

Install Sublime Text 3.

Install Mavens Mate.

Open up Sublime Text 3, and then open up Mavens Mate on your machine. There should be a Mavens Mate icon, at the top bar of your screen. Click it and then click "Plugins".

On the next window, click "Install Plugin". This installs the Mavens Mate plugin into Sublime Text 3.

Close and reopen Sublime Text 3.

Click Mavens Mate > Settings > User at the top Sublime Text 3 Toolbar.

This will open a file called mavensmate.sublime-settings. Paste the following in that file. Adjust to the path and directory you want to store your salesforce code in. It must be an absolute path.

{
  "mm_workspace": "/Users/your_username/code/salesforce"
}

Save and close that file.

Clone the repo

cd /Users/your_username/code/salesforce
git clone https://github.com/sendgrid/sendgrid-apex.git

Open the project

With Sublime Text 3 still open, click "File > Open", and open the sendgrid-apex folder.

Create the project on Salesforce

Right click on the sendgrid-apex containing folder, and choose "MavensMate > Create MavensMate Project".

On the next screen enter the following:

  • For project name: 'sendgrid-apex'
  • For username: enter your email address
  • For password: enter your password APPENDED with your salesforce security token. (see above for how to generate the security token)

When you're ready click the 'Create Project' button.

This will create the project up on Salesforce.com and locally on your machine. That's what we want.

Next, the final and most important step.

Click "Mavens Mate > Project > Compile Project".

You're done

You're done!

Now go ahead and develop. I recommend this tutorial or this blog post for learning some basics of developing with Apex.

Additional: Creating the unmanaged package.

Search for "packages".

Click "Create > Packages".

Click "New" under Packages.

Name the package "sendgrid-apex" and click save.

On the list of packages screen, now click "sendgrid-apex".

On the next screen under components, click "Add".

On the next screeen choose "Apex Class" under Component Type.

Check all the SendGrid related classes.

Then click "Add to Package".

Then click "Add" again.

This time on the next screen choose "Remote Site".

Check the SendGrid Remote Site, and click "Add to Package".

Then on the package show page, click "Upload".

On the next screen, enter "052114-sendgrid-apex" for the name (change the mm/dd/yy to the current).

Set version number to 1.0.

Set the description to: "This Apex Toolkit allows you to quickly and easily send emails through SendGrid using Salesforce Apex."

Scroll down to the very bottom and click "Upload".

Copy and paste the installation url and place in the README.

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

sendgrid-apex's People

Contributors

motdotla avatar brandonmwest avatar

Watchers

James Cloos avatar Gustavo Melendez 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.