4

I will be sending an email using Messaging.SingleEmailMessage. This will be done from a controller action method.

After sending an email, I also need to save it for records purpose. Please note the email is sent from org wide email address to an email address outside of SF org.

Is it possible to save the email sent as an attachment to custom object record ?

  • how good are you with apex, one suggestion would be to create an inbound email listener and when you send the email out send it to your email listener and create a record from your inbound listener. – Rao Jun 2 '14 at 22:59
  • @rao has a good idea - remember to bcc it to the inbound email handler email address otherwise reply all's will get logged as well. – cropredy Jul 3 '14 at 1:11
1

To save the email as an attachment, you need to decide the format. If we assume you want to save as a text file, just make a new Attachment record, set the title and the body, and save it. You'll probably want to concatenate the various email fields into the attachment. For instance, the attachment name would be

EmailSubject - SentDateTime.txt

And the body would be

From: <FromAddress>
To: <ToAddress>
Subject: <EmailSubject>

<EmailBody>

etc...

So the code could be:

Attachment a = new Attachment();
a.name = e.subject + ' ' + datetime.now() + .txt;
a.ParentId = [However you're going to figure this out for the parent record];
a.body = e.textbody;
insert a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.