You need to sign in to do that
Don't have an account?

send an Email using a visualforce template, with a pdf attachment
Hello,
I am trying to send an Email using a visualforce template, with a pdf attachment, but can't seem to make it work. In my application i have a "sutTer__c" object, which is linked to a "traveller" object. This is a simplified version of the Email template:
<messaging:emailTemplate subject="TER" recipientType="User" relatedToType="sutTer__c">
<messaging:plainTextEmailBody >
Attached the TER details
</messaging:plainTextEmailBody>
<messaging:attachment renderAs="pdf" filename="ter.pdf">
This is the Attachment fot Ter number
<apex:outputText value="{!relatedTo.name}"/>
</messaging:attachment>
</messaging:emailTemplate>
This is the code that should send the Email (at this point there is an object ter of the type sutTer__c):
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {ter.Traveller__r.user__r.Email});
mail.setTemplateId('00X800000018i7A');
mail.setWhatId(ter.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
However, when i try to send an Email with this code, i get an error massage:
"Missing targetObjectId with template."
So i set targetObjectId (and set the saveAsActivity to false,to prevent another error massage), and only then send the mail:
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {ter.Traveller__r.user__r.Email});
mail.setTemplateId('00X800000018i7A');
mail.setWhatId(ter.id);
mail.setTargetObjectID(ter.traveller__r.user__r.id);
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
in this case the email is being sent, but it is empty (no body and no attachment). Can anyone tell me what am I doing wrong?
thanks,
Alon
I am having the same issue. Blank emails being sent. It is picking up the subject from the template, so it's doing something, but no body. Anyone with any ideas?