メール添付ファイルの作成
メールに添付ファイルを追加する場合、必要なのはカスタムコントローラに数行のコードを追加することだけです。メール添付ファイルのファイルの種類は Blob です。添付ファイルを作成するには、Apex Messaging.EmailFileAttachment クラスを使用する必要があります。ファイル名と EmailFileAttachment オブジェクトの内容の両方を定義する必要があります。
PDF 添付ファイルの追加
次の例は、PDF として表示される Visualforce ページへの PageReference をメール添付ファイルに変換する方法を示します。最初に、attachmentPDF というページを作成します。
1<apex:page standardController="Account" renderAs="PDF">
2
3 <h1>Account Details</h1>
4
5 <apex:panelGrid columns="2">
6
7 <apex:outputLabel for="Name" value="Name"/>
8 <apex:outputText id="Name" value="{!account.Name}"/>
9
10 <apex:outputLabel for="Owner" value="Account Owner"/>
11 <apex:outputText id="Owner" value="{!account.Owner.Name}"/>
12
13 <apex:outputLabel for="AnnualRevenue" value="Annual Revenue"/>
14 <apex:outputText id="AnnualRevenue" value="{0,number,currency}">
15 <apex:param value="{!account.AnnualRevenue}"/>
16 </apex:outputText>
17
18 <apex:outputLabel for="NumberOfEmployees" value="Employees"/>
19 <apex:outputText id="NumberOfEmployees" value="{!account.NumberOfEmployees}"/>
20
21 </apex:panelGrid>
22
23</apex:page>次に、カスタムコントローラの send() メソッドで EmailFileAttachment オブジェクトを作成します。次の例は、Messaging.sendEmail のコールよりも前に配置する必要があります。
SingleEmailMessage オブジェクトの名前が email の場合、添付ファイルを次のように関連付けます。
1// Reference the attachment page, pass in the account ID
2 PageReference pdf = Page.attachmentPDF;
3 pdf.getParameters().put('id',(String)account.id);
4 pdf.setRedirect(true);
5
6 // Take the PDF content
7 Blob b = pdf.getContent();
8
9 // Create the email attachment
10 Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
11 efa.setFileName('attachment.pdf');
12 efa.setBody(b);1email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});カスタムコンポーネントの添付ファイルとしての定義
カスタムコンポーネントを作成して Visualforce メールフォームで使用し、メールの PDF を表示することで、ユーザは送信しようとしている内容のプレビューを表示できます。
次のマークアップは、メールの添付ファイルを表す、attachment という名前のカスタムコンポーネントを定義します。
1<apex:component access="global">
2 <h1>Account Details</h1>
3
4 <apex:panelGrid columns="2">
5
6 <apex:outputLabel for="Name" value="Name"/>
7 <apex:outputText id="Name" value="{!account.Name}"/>
8
9 <apex:outputLabel for="Owner" value="Account Owner"/>
10 <apex:outputText id="Owner" value="{!account.Owner.Name}"/>
11
12 <apex:outputLabel for="AnnualRevenue" value="Annual Revenue"/>
13 <apex:outputText id="AnnualRevenue" value="{0,number,currency}">
14 <apex:param value="{!account.AnnualRevenue}"/>
15 </apex:outputText>
16
17 <apex:outputLabel for="NumberOfEmployees" value="Employees"/>
18 <apex:outputText id="NumberOfEmployees" value="{!account.NumberOfEmployees}"/>
19
20 </apex:panelGrid>
21</apex:component>
attachmentPDF ページを次のように置き換えます。
1<apex:page standardController="account" renderAs="PDF">
2 <c:attachment/>
3</apex:page>次に、前の sendEmailPage の下部に表示するカスタムコンポーネントを追加します。
1<apex:pageBlock title="Preview the Attachment for {!account.name}">
2 <c:attachment/>
3</apex:pageBlock>添付ファイルとプレビューの両方を変更する場合、attachment カスタムコンポーネントを変更するのは、どちらか一方の場所のみで済みます。
例: 添付ファイル付きメールの送信
次の例では、前の sendEmail の例に、Visualforce ページを添付ファイルとして追加するカスタムコンポーネントが加えられています。最初に、コントローラが次の処理を行います。
1public class sendEmail {
2 public String subject { get; set; }
3 public String body { get; set; }
4
5 private final Account account;
6
7 // Create a constructor that populates the Account object
8 public sendEmail() {
9 account = [SELECT Name,
10 (SELECT Contact.Name, Contact.Email FROM Account.Contacts)
11 FROM Account
12 WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
13 }
14
15 public Account getAccount() {
16 return account;
17 }
18
19 public PageReference send() {
20 // Define the email
21 Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
22
23 // Reference the attachment page and pass in the account ID
24 PageReference pdf = Page.attachmentPDF;
25 pdf.getParameters().put('id',(String)account.id);
26 pdf.setRedirect(true);
27
28 // Take the PDF content
29 Blob b = pdf.getContent();
30
31 // Create the email attachment
32 Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
33 efa.setFileName('attachment.pdf');
34 efa.setBody(b);
35
36 String addresses;
37 if (account.Contacts[0].Email != null) {
38 addresses = account.Contacts[0].Email;
39 // Loop through the whole list of contacts and their emails
40 for (Integer i = 1; i < account.Contacts.size(); i++) {
41 if (account.Contacts[i].Email != null) {
42 addresses += ':' + account.Contacts[i].Email;
43 }
44 }
45 }
46
47 String[] toAddresses = addresses.split(':', 0);
48
49 // Sets the paramaters of the email
50 email.setSubject( subject );
51 email.setToAddresses( toAddresses );
52 email.setPlainTextBody( body );
53
54 email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
55
56 // Sends the email
57 Messaging.SendEmailResult [] r =
58 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
59
60 return null;
61 }
62}次に、Visualforce ページがメールを送信します。
1<apex:page controller="sendEmail">
2 <apex:messages/>
3 <apex:pageBlock title="Send an Email to Your {!account.name} Representatives">
4 <p>Fill out the fields below to test how you might send an email to a user.</p>
5
6 <apex:dataTable value="{!account.Contacts}" var="contact" border="1">
7 <apex:column>
8 <apex:facet name="header">Name</apex:facet>
9 {!contact.Name}
10 </apex:column>
11 <apex:column>
12 <apex:facet name="header">Email</apex:facet>
13 {!contact.Email}
14 </apex:column>
15 </apex:dataTable>
16
17 <apex:form><br/><br/>
18 <apex:outputLabel value="Subject" for="Subject"/>: <br/>
19 <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
20 <br/><br/>
21
22 <apex:outputLabel value="Body" for="Body"/>: <br/>
23 <apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
24 <br/><br/>
25
26 <apex:commandButton value="Send Email" action="{!send}"/>
27 </apex:form>
28 </apex:pageBlock>
29
30 <apex:pageBlock title="Preview the Attachment for {!account.name}">
31 <c:attachment/>
32 </apex:pageBlock>
33</apex:page>