function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mh1974mh1974 

Static PDF email attachment

Here's my dilema;  I have a visualforce page that allows a user to select a contact to send an email to.  This email will always be the same .pdf file attached to it.  I've learned how to create and send the email,  but,  I don't see how I can attach the .pdf file.  I thought that I would be able to save the file as a static resource and add it,  but,  that does not seem possible.  I've read lots of material on converting a visualforce page into a pdf and adding that as an attachment,  but,  that doesn't really fit with what I'm doing.  My file will always be the same,  so,  I was really looking for a way to store it in 'the cloud' and then add it to my emails when required.

 

Any advice?

Best Answer chosen by Admin (Salesforce Developers) 
mh1974mh1974

Okay, here's how I did it.

 

public void SendTestEmail(){
    //Create a new single email message object
    Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
   
    //Address
    objEmail.setToAddresses(new String[] {'email@somewhere.com'});
   
    // Subject
    objEmail.setSubject('PDF Test email');            
                   
    //Content
    objEmail.setPlainTextBody('This is a test email');
   
    //Attachments
    List<StaticResource> objPDF = [Select body, name from StaticResource where Name = 'xx'];
   
    Messaging.EmailFileAttachment[] objEmailAttachments = new Messaging.EmailFileAttachment[1];
    Messaging.EmailFileAttachment objPDFAttachment = new Messaging.EmailFileAttachment();
    objPDFAttachment.setBody(objPDF[0].Body);
    objPDFAttachment.setFileName(objPDF[0].name + '.pdf');
    objEmailAttachments[0] = objPDFAttachment;
    objEmail.setFileAttachments(objEmailAttachments);
         
    //Send

    List<Messaging.Sendemailresult> objER = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {objEmail});
   
    if (!objER[0].isSuccess()){
        Error handling;
    }
}

All Answers

mh1974mh1974

Come on,  I can't be the only person that needs to do this!

 

Answers please!

mh1974mh1974

Okay, here's how I did it.

 

public void SendTestEmail(){
    //Create a new single email message object
    Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
   
    //Address
    objEmail.setToAddresses(new String[] {'email@somewhere.com'});
   
    // Subject
    objEmail.setSubject('PDF Test email');            
                   
    //Content
    objEmail.setPlainTextBody('This is a test email');
   
    //Attachments
    List<StaticResource> objPDF = [Select body, name from StaticResource where Name = 'xx'];
   
    Messaging.EmailFileAttachment[] objEmailAttachments = new Messaging.EmailFileAttachment[1];
    Messaging.EmailFileAttachment objPDFAttachment = new Messaging.EmailFileAttachment();
    objPDFAttachment.setBody(objPDF[0].Body);
    objPDFAttachment.setFileName(objPDF[0].name + '.pdf');
    objEmailAttachments[0] = objPDFAttachment;
    objEmail.setFileAttachments(objEmailAttachments);
         
    //Send

    List<Messaging.Sendemailresult> objER = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {objEmail});
   
    if (!objER[0].isSuccess()){
        Error handling;
    }
}

This was selected as the best answer
Deepak PansariDeepak Pansari

Hey,

 

I am using the same code which you have mentioned above.

 

I want to Send an email to Opportunty owner with the Attachment which are added into opportunity.

 

I an getting emial with attachments but attachment file converted into binary file.

 

Any Idea ??? 

Rasmus MenckeRasmus Mencke
a pdf document is a binary file. All attachments are send as octent-stream for now. With Winter '10 you can set the content-type to be the right one for an email attachment.
Deepak PansariDeepak Pansari

Thanks!!

 

That means , we can not attach any file with email as it gets converted into binary

 

 

mh1974mh1974
I'm confused,  are you saying that when you run the code it sends the file to the email address,  but,  the file is not a pdf?  I have just retested this and I receive a pdf file that opens without any problem.
Deepak PansariDeepak Pansari

 

Hi,

 

I have written a trigger on Attachment and it will send an email with attachments .

I uploaded a file say “abc.txt” and I got this file into email but it getting converted into binary( when I open this txt file it shows junk characters)

 

 

Below is my trigger code 

 

trigger AttchTrg on Attachment (after insert, after update) {

public Attachment[] newAttachment = Trigger.new;  // Adding an Attachment 

        // Create a new single email message object

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        String[] toAddresses = new String[] {'test@test.com'};

        mail.setToAddresses(toAddresses);

        // Specify the subject line for your email address.

        mail.setSubject('Test email');

        mail.setHtmlBody('<b>testing</b>');

        // add the attachment to an email message

        Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];

        //create an attachment object to populate with data

        Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();

        fileAttachment.setBody(newAttachment[0].Body);

        fileAttachment.setFileName(newAttachment[0].Name); 

        //add attachment object to attachments array

        fileAttachments[0]=fileAttachment;

        //add attachments to mail message

        mail.setFileAttachments(fileAttachments); 

        // Send the email you have created.

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    } 

    

Let me know.

 

Message Edited by Deepak Pansari on 08-04-2009 06:11 AM
mh1974mh1974

I can't see anything particularly wrong with this, except that I found I had to append the file type when I called the setFileName method, e.g.

 

fileAttachment.setFileName(newAttachment[0].Name + '.txt');

 

but,  that was only so I didn't have to browse to the right application to open the file.

 

I tested your code sucesfully with the following changes:

1. I did not call the code through a trigger - I simply used a VF page

2. I used a text file I uploaded as a static resource.

 

I'm not suggesting you make those changes,  it just points to a possible problem with running this code through a trigger or how you're coming by the attachment.

 

Matt

jaya sai prasadjaya sai prasad
Hi,

you have a problem with the email attachments, then please check this native salesforce application MassMailer DOCS.
 
MassMailer DOCS let’s you easily mass email attachments to your leads or contacts while securely storing your files with Rackspace Cloud Files.

You can try this app by installing from appexchange  - Massmailer Docs

You can learn more details about the product on this website - docs.massmailer.io