Email a Document with

After my last post, Create and Email a PDF with, I received a few comments whether it was possible to do the same with Document stored in Could you choose a Document and then send it via email as an attachment? The short answer is yes! However, I tried to do it from a Sites page but was not able to get it to work. The document is not marked for internal use and it is marked as an externally available image. I also made sure that the public settings for my Site included read access to documents but still the query for the document returns no results. No time to look at it in depth right now so if anyone has an idea, please send it my way.

DocumentEmailer Visualforce Page

<apex:page controller="DocumentEmailController">
 <apex:sectionHeader title="Document Example" subtitle="Email a Document" 
  description="Example of how to email a Document."/>

 <apex:form >
  <apex:pageMessages />
  <apex:pageBlock title="Document Input">
 <apex:pageBlockButtons >
  <apex:commandButton action="{!sendDoc}" value="Send Document"/>
 <apex:pageBlockSection >
  <apex:pageBlockSectionItem >
  <apex:outputLabel value="Email to send to" for="email"/>
   <apex:inputText value="{!email}" id="email"/>
  <apex:pageBlockSectionItem >
  <apex:outputLabel value="Document" for="document"/>
  <apex:selectList value="{!documentId}" id="document" size="1">
   <apex:selectOptions value="{!documents}"/>




public with sharing class DocumentEmailController {

 public ID documentId {get;set;}
 public String email {get;set;}
 public List<SelectOption> documents {
  get {
 if (documents == null) {
  documents = new List<SelectOption>();
  documents.add(new SelectOption('01570000001NZDn','Cup of Coffee? - DOC'));
  documents.add(new SelectOption('01570000001NZDi','Workflow Cheatsheet - PDF'));
 return documents;
 public PageReference sendDoc() {

  Document doc = [select id, name, body, contenttype, developername, type 
 from Document where id = :documentId];
  Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
  attach.Body = doc.Body;

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setToAddresses(new String[] { email });
  mail.setSubject('Document Email Demo');
  mail.setHtmlBody('Here is the email you requested: ';
  mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
  // Send the email
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Document sent to '+email));

  return null;



Test Class

private class Test_DocumentEmailer {

 static Document document;

 static {
  document = new Document();
  document.Body = Blob.valueOf('Some Text');
  document.ContentType = 'application/pdf';
  document.DeveloperName = 'my_document';
  document.IsPublic = true;
  document.Name = 'My Document';
  document.FolderId = [select id from folder where name = 'My Test Docs'].id;
  insert document;

 static testMethod void testDocumentEmailer() {

  PageReference pref = Page.DocumentEmailer;  
  DocumentEmailController con = new DocumentEmailController();  
  // populate the field with values
  con.documentId =; = '';
  // submit the request
  pref = con.sendDoc();