When we teach the advanced developer class, we go over code for sending an email using the native Apex send email methods. The big problem with this approach is that emails sent this way count against the daily limit of mass emails within Salesforce.com. This means that, at most, you can send 1,000 emails per day using this pattern. For most organizations, this is fine, but for organizations sending large volumes of email, 1,000 emails per day is inadequate.
People have asked if enabling Email Relay overcomes this limit. Unfortunately, it does not. There is a native work around to this limit using workflow rules, but that work around doesn't work for customer portal users, authenticated website users or sites guest users. No native workaround exists that allows you to fully overcome this limit.
I needed to overcome the limit. I found a very reasonable solution called Elastic Email (http://www.elasticemail.com) that has a simple REST API you can use to send email. There rate starts at $.001 per email and goes down from there. Basically, instead of calling the native send mail, you call Elastic Email. You have to set them up as a remote site before using this code.
//Force.com
//Sample of using elastic email
//Emails sent with standard force.com calls count against daily mass email limit
public with sharing class ElasticEmail {
private static final String API_KEY = '##-Set the key here-##';
private static final String USER_NAME = '## Set your username here ####';
//overload method to mask the need to send username and key but kept the original for flexibility
public static String SendElasticEmail(String fromEmail, String fromName,
String subject, String body, String toEmail) {
return ElasticEmail.SendElasticEmail(USER_NAME, API_KEY, fromEmail, fromName,
subject, body, toEmail);
}
//send the email
public static String SendElasticEmail(String userName, String APIKey, String fromEmail,
String fromName, String subject, String body, String toEmail) {
Http http = new Http();
HttpRequest req = new HttpRequest();
HTTPResponse res;
req.setEndpoint('https://api.elasticemail.com/mailer/send');
req.setMethod('POST');
try {
//Construct the data
String data = 'userName=' + userName;
data += '&api_key=' + APIKey;
data += '&from=' + fromEmail;
data += '&from_name=' + fromName;
data += '&subject=' + EncodingUtil.urlEncode(subject, 'UTF-8');
data += '&body_text=' + EncodingUtil.urlEncode(body, 'UTF-8');
data += '&to=' + toEmail;
req.setBody(data);
res = http.send(req);
//Send data
System.debug(res.getBody());
return res.getBody();
}
catch(Exception e) {
return null;
}
}
//a simplified method to send an email to a user
public static String sendEmail(User u) {
return ElasticEmail.SendElasticEmail('customer.service@mydomain.com', 'Customer Service',
'Subject', ElasticEmail.EmailBody(u), u.Email);
}
public static String EmailBody(User u) {
String eBody = 'Dear ' + u.FirstName + ',\n\n';
eBody += 'You are receiving this email with Elastic Email.\n\n';
eBody += 'Sincerly,\n\n Customer Service';
return eBody;
}
static testMethod void testElasticEmail() {
User u = [Select FirstName, Id from User Where Id = : UserInfo.getUserId()];
ElasticEmail.sendEmail(u);
}
}
6 comments:
Great Post !!!!
Thanks Steve
Bhup
Hello Steve,
When i sending bulk (more then 10)Emails with this code it throw a error "Callout" and when i put the code in @future class then it throw a error 10 Coullout is possible.
Can we can bulk Email from Elastic Email?
Take look at MassMailer app at -
http://www.mansasys.com/massmailer
which will let you send mass emails without any limitations. It does have api as well.
http://support.mansasys.com/support/solutions/articles/72737-mass-mailer-developer-guide
nice...
ibm-message-broker training in chennai
good...
oracle-apps-finance training in chennai
This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...thanks lot!!
Android Training in Chennai
Android Online Training in Chennai
Android Training in Bangalore
Android Training in Hyderabad
Android Training in Coimbatore
Android Training
Android Online Training
Post a Comment