メール添付ファイルの作成
メールに添付ファイルを追加する場合、必要なのはカスタムコントローラに数行のコードを追加することだけです。メール添付ファイルのファイルの種類は 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 ページを添付ファイルとして追加するカスタムコンポーネントが加えられています。最初に、コントローラが次の処理を行います。
1swfobject.registerObject("clippy.codeblock-6", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class sendEmail {
18 public String subject { get; set; }
19 public String body { get; set; }
20
21 private final Account account;
22
23 // Create a constructor that populates the Account object
24 public sendEmail() {
25 account = [SELECT Name,
26 (SELECT Contact.Name, Contact.Email FROM Account.Contacts)
27 FROM Account
28 WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
29 }
30
31 public Account getAccount() {
32 return account;
33 }
34
35 public PageReference send() {
36 // Define the email
37 Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
38
39 // Reference the attachment page and pass in the account ID
40 PageReference pdf = Page.attachmentPDF;
41 pdf.getParameters().put('id',(String)account.id);
42 pdf.setRedirect(true);
43
44 // Take the PDF content
45 Blob b = pdf.getContent();
46
47 // Create the email attachment
48 Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
49 efa.setFileName('attachment.pdf');
50 efa.setBody(b);
51
52 String addresses;
53 if (account.Contacts[0].Email != null) {
54 addresses = account.Contacts[0].Email;
55 // Loop through the whole list of contacts and their emails
56 for (Integer i = 1; i < account.Contacts.size(); i++) {
57 if (account.Contacts[i].Email != null) {
58 addresses += ':' + account.Contacts[i].Email;
59 }
60 }
61 }
62
63 String[] toAddresses = addresses.split(':', 0);
64
65 // Sets the paramaters of the email
66 email.setSubject( subject );
67 email.setToAddresses( toAddresses );
68 email.setPlainTextBody( body );
69
70 email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
71
72 // Sends the email
73 Messaging.SendEmailResult [] r =
74 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
75
76 return null;
77 }
78}次に、Visualforce ページがメールを送信します。
1swfobject.registerObject("clippy.codeblock-7", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="sendEmail">
18 <apex:messages/>
19 <apex:pageBlock title="Send an Email to Your {!account.name} Representatives">
20 <p>Fill out the fields below to test how you might send an email to a user.</p>
21
22 <apex:dataTable value="{!account.Contacts}" var="contact" border="1">
23 <apex:column>
24 <apex:facet name="header">Name</apex:facet>
25 {!contact.Name}
26 </apex:column>
27 <apex:column>
28 <apex:facet name="header">Email</apex:facet>
29 {!contact.Email}
30 </apex:column>
31 </apex:dataTable>
32
33 <apex:form><br/><br/>
34 <apex:outputLabel value="Subject" for="Subject"/>: <br/>
35 <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
36 <br/><br/>
37
38 <apex:outputLabel value="Body" for="Body"/>: <br/>
39 <apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
40 <br/><br/>
41
42 <apex:commandButton value="Send Email" action="{!send}"/>
43 </apex:form>
44 </apex:pageBlock>
45
46 <apex:pageBlock title="Preview the Attachment for {!account.name}">
47 <c:attachment/>
48 </apex:pageBlock>
49</apex:page>