function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kieran.osullivankieran.osullivan 

Can extensions to standard controllers send E-Mails

I want a Visual Force Page to send an e-mail when it is opened and I tried to do this with an extension but it did not work.

 

Can an extension to a VFP which uses a standard controller send an e-mail?

 

I have tried the following.

 

1. The debug logs appear to show the messages being sent.

2. My triggers send e-mails to the same address so there is nothing wrong there.

3. I have sent messages to different servers so it is not the server.

4. The extension code executes without errors.

5. The e-mail addresses are correct.

 

My code is below, any suggestions would be grate thanks?

 

Visual Force Page
<apex:page sidebar="false" showHeader="false" extensions="EmailPDFController2" standardController="mycompany_Invoice__c">
<p>Sending Email</p>
</apex:page>

APEX Class
public class EmailPDFController2 {
    public EmailPDFController2 (ApexPages.StandardController controller){    
         try {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'test2@test2.com'};
            String[] CopyToAddresses = new String[]{'test@test1.com','test3@test3.com'};
            mail.setToAddresses(toAddresses);
            mail.setCcAddresses(CopyToAddresses);
            String msg = 'Hellow';            
            mail.setPlainTextBody(msg);
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        } catch (Exception e){}
    }
}


Log Entrhy

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
11:30:10.071 (71679000)|EXECUTION_STARTED
11:30:10.071 (71737000)|CODE_UNIT_STARTED|[EXTERNAL]|066U0000000GXQs|VF: /apex/mycompanyInvoicePDF
11:30:10.074 (74704000)|CODE_UNIT_STARTED|[EXTERNAL]|01pU00000012Pwn|EmailPDFController2 <init>
11:30:10.074 (74722000)|SYSTEM_MODE_ENTER|true
11:30:10.075 (75730000)|METHOD_ENTRY|[1]|01pU00000012Pwn|EmailPDFController2.EmailPDFController2()
11:30:10.075 (75828000)|METHOD_EXIT|[1]|EmailPDFController2
11:30:10.076 (76837000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
11:30:10.076 (76875000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
11:30:10.076 (76910000)|SYSTEM_METHOD_ENTRY|[5]|LIST<String>.add(Object)
11:30:10.076 (76930000)|SYSTEM_METHOD_EXIT|[5]|LIST<String>.add(Object)
11:30:10.076 (76944000)|SYSTEM_CONSTRUCTOR_ENTRY|[6]|<init>()
11:30:10.076 (76965000)|SYSTEM_CONSTRUCTOR_EXIT|[6]|<init>()
11:30:10.076 (76985000)|SYSTEM_METHOD_ENTRY|[6]|LIST<String>.add(Object)
11:30:10.077 (77000000)|SYSTEM_METHOD_EXIT|[6]|LIST<String>.add(Object)
11:30:10.077 (77018000)|SYSTEM_METHOD_ENTRY|[6]|LIST<String>.add(Object)
11:30:10.077 (77032000)|SYSTEM_METHOD_EXIT|[6]|LIST<String>.add(Object)
11:30:10.077 (77270000)|SYSTEM_CONSTRUCTOR_ENTRY|[11]|<init>()
11:30:10.077 (77302000)|SYSTEM_CONSTRUCTOR_EXIT|[11]|<init>()
11:30:10.077 (77324000)|SYSTEM_METHOD_ENTRY|[11]|LIST<Messaging.SingleEmailMessage>.add(Object)
11:30:10.077 (77342000)|SYSTEM_METHOD_EXIT|[11]|LIST<Messaging.SingleEmailMessage>.add(Object)
11:30:10.077 (77389000)|SYSTEM_METHOD_ENTRY|[11]|Messaging.sendEmail(LIST<Messaging.Email>)
11:30:10.085 (85895000)|EMAIL_QUEUE|[11]|bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [test2@test2.com], ccAddresses: [test@test1.com, test3@test3.com], plainTextBody: Hellow,
11:30:10.086 (86044000)|SYSTEM_METHOD_EXIT|[11]|Messaging.sendEmail(LIST<Messaging.Email>)
11:30:10.086 (86068000)|CODE_UNIT_FINISHED|EmailPDFController2 <init>
11:30:10.649 (89096000)|CUMULATIVE_LIMIT_USAGE
11:30:10.649|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 8 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 1 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:30:10.649|CUMULATIVE_LIMIT_USAGE_END

11:30:10.089 (89131000)|CODE_UNIT_FINISHED|VF: /apex/mycompanyInvoicePDF
11:30:10.089 (89143000)|EXECUTION_FINISHED

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kieran.osullivankieran.osullivan

The solution to this was to remove the code from the constructor of the extension and put it in a function which was then aclled by a button on the VFP.  This then called a second VFP which rendered as PDF.  The code is below.

 

Page 1 contains button called "Send Me as PDF"

Page 2 is page 1 with the renderas=""PDF" in the heading.

 

APEX Code Here

 

public with sharing class EmailPDFController {
    public EmailPDFController (ApexPages.StandardController controller){ }
    public void SendThisEmail(){
         try {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'firstname.lastname@MyCompany.ie'};
            String[] CopyToAddresses = new String[]{'firstname.lastname@test.com','firstname.lastname@MyCompanygroup.eu'};
            mail.setToAddresses(toAddresses);
            mail.setCcAddresses(CopyToAddresses);

            String msg = 'Hellow';            
            
            PageReference curpage = ApexPages.currentPage();
            PageReference pdfPage = Page.MyCompanyInvoicePDF2;
            String ParID = (string) curpage.getParameters().get('id');
            pdfPage.getParameters().put('id',ParID);
            Blob b = pdfPage.getContent();
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
            efa.setBody(b); //attach the PDF
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            
            mail.setPlainTextBody(msg);
            // mail.setHtmlBody(msg);
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            System.debug('Email Results isSuccess = ' +  r[0].IsSuccess());
            Messaging.SendEmailError [] err_mail = r[0].getErrors();
        } catch (Exception e){System.debug('ERROR:' + e);}    
    }
}

All Answers

neophyteneophyte

You can send emails from extensions. No issues in that.

 

It does seem from you debug log that mails are being sent. But just for confimation check the following:

 

1)Check email addresses

2)Check the result set ( Messaging.SendEmailResult )

3)Add debug statements in catch block.

 

-Anand

 

PS: I have seen instances where the email server blocked mails from salesforce, or mails going to Junk. Please check.

 

 

kieran.osullivankieran.osullivan

Hi

I have already checked for problems with the msil servers I used two different e-mail addresses and the messages were not sent I also checked the mail result and it reports success with noerrors.

 

When the same e-mail addresses are used in a trigger it works fine.

 

SFFSFF

I see two issues with your code that, if solved, may help solve your issue:

 

public class EmailPDFController2 {
    public EmailPDFController2 (ApexPages.StandardController controller){    
         try {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'test2@test2.com'};
            String[] CopyToAddresses = new String[]{'test@test1.com','test3@test3.com'};
            mail.setToAddresses(toAddresses);
            mail.setCcAddresses(CopyToAddresses);
            String msg = 'Hellow';            
            mail.setPlainTextBody(msg);
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        } catch (Exception e){}
    }
}
  1. You are capturing the Messaging.SendEmailResult[] array but you aren't looking at it. If you want to know if your email went, you need to look at this array.
  2. You have a try-catch block - good! You are ignoring any exception that come up when sending the email - not good!

Fix these two issues and the source of the issue should become clear.

 

Good luck!

kieran.osullivankieran.osullivan

The solution to this was to remove the code from the constructor of the extension and put it in a function which was then aclled by a button on the VFP.  This then called a second VFP which rendered as PDF.  The code is below.

 

Page 1 contains button called "Send Me as PDF"

Page 2 is page 1 with the renderas=""PDF" in the heading.

 

APEX Code Here

 

public with sharing class EmailPDFController {
    public EmailPDFController (ApexPages.StandardController controller){ }
    public void SendThisEmail(){
         try {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'firstname.lastname@MyCompany.ie'};
            String[] CopyToAddresses = new String[]{'firstname.lastname@test.com','firstname.lastname@MyCompanygroup.eu'};
            mail.setToAddresses(toAddresses);
            mail.setCcAddresses(CopyToAddresses);

            String msg = 'Hellow';            
            
            PageReference curpage = ApexPages.currentPage();
            PageReference pdfPage = Page.MyCompanyInvoicePDF2;
            String ParID = (string) curpage.getParameters().get('id');
            pdfPage.getParameters().put('id',ParID);
            Blob b = pdfPage.getContent();
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
            efa.setBody(b); //attach the PDF
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            
            mail.setPlainTextBody(msg);
            // mail.setHtmlBody(msg);
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            System.debug('Email Results isSuccess = ' +  r[0].IsSuccess());
            Messaging.SendEmailError [] err_mail = r[0].getErrors();
        } catch (Exception e){System.debug('ERROR:' + e);}    
    }
}

This was selected as the best answer