+ Start a Discussion

VF Email template sent to user.Id vs contact.Id. Access external resources issue


I am stuck on something that I feel very strange. Any input is apprecaited.
We run a batch process that sends Order Confirmation Messaging.SingleEmailMessage. Emails.
2 Emails are sent using the same VF Template ( setTemplateID('00XW0000000QOD5') ) in the same Messaging.sendEmail(new Messaging.SingleEmailMessage[] { ordConEma , ordConEmaCopy }); using:
- ordConEma.setTargetObjectId(contact.Id) // the person who needs to receive the Email
- ordConEmaCopy.setTargetObjectId(user.id); //the Admin, in order to keep a copy of the Email in a mailbox
The VF Template includes:
- an messaging HTML Part
- an messaging attachment rendered as PDF, exact copy of the HTML part.
The VF Templates uses an Image Custom Component that brings back a contactId related URL from an external site.
The external site is declared in the Remote Site Conf.
This component is included in both the HTML Part and the attachment.
The problem:
The userId receiver receives the Email and the attachment perfectly, with both images displayed

The contactId receiver does NOT received the Email, although it is queued properly according to the Debug log.
Trying to pinpoint the problem:
IF I remove the Image Component from the messaging.attachment, if all works fine with both Emails (but obviously, I do not have the image in my PDF for the ContactId = my customer = no good)
IF I remove the external image server from the remote sites, both Email are sent properly, but obviously got a missing Picture icon in my PDF of the ContactId and it is no good either.
I do not understand why userId receiver works perfect and contactId cannot receive the Email, which is built properly according to the debug log.
Thanks in advance for your support. Best regards,
Jerome Hardy

Hi Jerome, 

There are two ways of sending images in email templates: Documents and Static Resources.

The problem with the document is that you have to keep the document ID. It is better to use a static resource. In order to have a static resource accessible in an email, it must meet certain criteria:

1. The static resource must be set [Cache-Control = Public].
2. You must use the sites URL as the server, this is why I used [http://bankofcyprusuk.force.com{!URLFOR(...)}]
3. The spelling of the Static Control must match exactly (it is case sensitive) to the path inside the ZIP file.