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
killertypokillertypo 

Unable to send email from VF + Apex in Sandbox

I have the following code: 

 

//
    // Will email the page as an attached PDF 
    // 
    public PageReference getForwardAsPDF() { 
        // Setup the email    
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        String [] toAddress = new String[] { 'test@email.com' }; 
        email.setToAddresses(toAddress); 
        email.setReplyTo('sandbox@test.com.maintest');   
        email.setSubject('From forwardAsPDF!');
        email.setPlainTextBody('Here is the body of the email'); 

        // Get the pdf-ized secondary page
        PageReference pdf = Page.ERAEmailAttachment; // Duplicate page - so i don't get the dreaded getContent() error
        pdf.getParameters().put('id', era.Id); 
        pdf.getParameters().put('asEmail', 'false');
        pdf.setRedirect(true);
        
        // Grab the content of said page 
        Blob b = pdf.getContent();                
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('ERAsomedatestring.pdf');
        efa.setBody(b);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        
        // Send the email
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        for ( Messaging.SendEmailResult result : r ) 
        {
            if ( !r[0].isSuccess () ) 
            {
                System.debug ( result  );
            }
            else
            {
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
                ApexPages.addMessage(msg);
            }
        }
        
        return null;
    }

 

 

And the following VF / PDF page

 

(I'll spare you the details of the form since it is massive) 

 

<apex:page standardController="ERA__c" extensions="ERAFormProcessor" renderAs="pdf" action="{!if($CurrentPage.parameters.asEmail == 'true', forwardAsPDF, null)}" >
....blah
</apex:page>

 

 

I have the page setup so that when I pass the parameter asEmail=true (as you can see above) it calls the forwardAsPDF method.  

 

The return from this method is that the email was sent successfully, but I have yet to receive anything in my inbox.  :( 

 

What am I missing here.  

Best Answer chosen by Admin (Salesforce Developers) 
killertypokillertypo

Ended up going with an email template / attatchment that way.  Works and gives me what I want, not sure what the dealio was with getting it to work the way outlined above.

 

All Answers

Starz26Starz26

Maybe because the email address you are sending it to is: 'test@email.com'

 

is that really your email address?

killertypokillertypo

no, i swapped that out for the sake of this forum board ;) 

killertypokillertypo

I wanted to add - while I was unable to get this example working, i was able to get a usable / working solution with an email template / custom controller / and a little bit of apex behind the scenes to get it all going. 

 

:)  

 

I think it will end up meeting the needs of the users much better as well.  

 

Regards,

Mike 

killertypokillertypo

whoops didn't mean to mark that as the solution! >_< 

 

So i guess either way I can get it "work" i mean i can render the VF page, i can even use the UI to send the email templates out in a testing mode.  I can't for some reason get the code to generate the emails and it is driving me quite insane!  

 

Any input would be greatly appreciated!! 

AasifAasif

May be it is going into junk folder. Did u happen to check there.

killertypokillertypo

:( checked checked and double checked. 

 

I even sent it to multiple addresses, personal, work...etc 

 

Everything says that the email was sent successfully, i can obviously tell something happened.  I will get a log dump today and share, since after perusing that, i was a bit confused and how to properly read that sucker. 

killertypokillertypo

Ended up going with an email template / attatchment that way.  Works and gives me what I want, not sure what the dealio was with getting it to work the way outlined above.

 

This was selected as the best answer