You need to sign in to do that
Don't have an account?
Caleb_Sidel
Maybe you'll find this code snippet useful in using Apex Send Email
/*
* ==========================================================================
* Copyright (C) 2009 Caleb Sidel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ==========================================================================
*/
public class EmailUtils {
public static void sendEmailWithStandardAttachments(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Id> attachmentIDs) {
List<Attachment> stdAttachments = [SELECT id, name, body FROM Attachment WHERE Id IN:attachmentIDs];
sendEmailWithStandardAttachments(recipients, emailSubject, body, useHTML, stdAttachments);
}
public static void sendEmailWithStandardAttachments(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Attachment> stdAttachments) {
List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
for(Attachment attachment : stdAttachments) {
Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
fileAttachment.setFileName(attachment.Name);
fileAttachment.setBody(attachment.Body);
fileAttachments.add(fileAttachment);
}
sendEmail(recipients, emailSubject, body, useHTML, fileAttachments);
}
public static void sendTextEmail(List<String> recipients,String emailSubject,String textBody) {
sendEmail(recipients, emailSubject, textBody, false, null);
}
public static void sendHTMLEmail(List<String> recipients,String emailSubject,String htmlBody) {
sendEmail(recipients, emailSubject, htmlBody, true, null);
}
public static void sendEmail(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Messaging.EmailFileAttachment> fileAttachments) {
if(recipients == null) return;
if(recipients.size() == 0) return;
// Create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//the email is not saved as an activity.
mail.setSaveAsActivity(false);
// Assign the addresses for the To lists to the mail object.
mail.setToAddresses(recipients);
// Specify the subject line for your email address.
mail.setSubject(emailSubject);
// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);
if (useHTML) {
// Specify the html content of the email.
mail.setHtmlBody(body);
} else {
// Specify the text content of the email.
mail.setPlainTextBody(body);
}
// Specify FileAttachments
if(fileAttachments != null && fileAttachments.size() > 0) {
mail.setFileAttachments(fileAttachments);
}
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
static testMethod void testsendEmail_test() {
system.debug('## Start the testSendSupportEmails_test');
List<String> recepients=new String[]{'test@test.com','test2@test.com'};
sendTextEmail(recepients,'Test method', 'This is to test the sendTextNotificationEmail method');
sendHTMLEmail(recepients,'Test method', 'This is to test the sendTextNotificationEmail method');
}
static testMethod void testsendEmailNoReceipients_test() {
system.debug('## Start the testSendSupportEmails_test');
List<String> recepients=null;
sendTextEmail(recepients,'Test method', 'This is to test the sendTextNotificationEmail method');
recepients=new List<String>();
sendHTMLEmail(recepients,'Test method', 'This is to test the sendTextNotificationEmail method');
}
static testMethod void testsendEmailWithAttachment_test() {
system.debug('## Start the testSendSupportEmails_test');
List<String> recepients=new String[]{'test@test.com','test2@test.com'};
List<Attachment> stdAttachments = new List<Attachment>();
Attachment a = new Attachment();
a.Name = 'Test';
a.Body = EncodingUtil.base64Decode('Test Body');
stdAttachments.add(a);
sendEmailWithStandardAttachments(recepients,'Test method', 'This is to test the sendTextNotificationEmail method',false,stdAttachments);
}
static testMethod void testsendEmailWithAttachmentIDs_test() {
system.debug('## Start the testSendSupportEmails_test');
List<String> recepients=new String[]{'test@test.com','test2@test.com'};
List<ID> stdAttachments = new List<ID>();
Account acct = new Account(name='Test Account');
insert acct;
Attachment a = new Attachment();
a.ParentId = acct.Id;
a.Name = 'Test';
a.Body = EncodingUtil.base64Decode('Test Body');
insert a;
stdAttachments.add(a.Id);
sendEmailWithStandardAttachments(recepients,'Test method', 'This is to test the sendTextNotificationEmail method',false,stdAttachments);
}
}
Glenn at Mvista
I have been trying to get more than one attachment added to an email using a trigger. This class seems to do that, but I was not able to adjust your code to work in the trigger. So let me ask a really basic question, if I use this class, how to a "trigger" it so the email gets sent?