+ Start a Discussion
Aidel BruckAidel Bruck 

Email not sending from trigger

Hello all, 
I am really having trouble for the past week and have not gotten any solutions thus far.

I wrote a trigger to send an email every time a file is uploaded, 
It has 100% coverage and from the log it is apparent that the email is sent out
The setting for deliveribilty is set to all emails, and I tested deliveribility with success.
And yet I am not getting the email

Can anybody help me figure out why??

This is the code
trigger NewFileAlert on ContentDocument (after insert) 
{
// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (ContentDocument newDoc : Trigger.new) 
  {
     // Step 1: Create a new Email
          Messaging.SingleEmailMessage mail = 
          new Messaging.SingleEmailMessage();
    
          // Step 2: Set list of people who should get the email
          List<String> sendTo = new List<String>();
          sendTo.add('test@gmail.com');
....
          mail.setToAddresses(sendTo);
    
          // Step 3: Set who the email is sent from
          mail.setReplyTo('me@gmail.com');
          mail.setSenderDisplayName('Aidel Bruck');
    
       
          // Step 4. Set email contents - you can use variables!
          mail.setSubject('New Document Added');
          String body = 'Please note: A new document has been added/n  ';
          body += 'File name: '+ newdoc.title+'/n' ;
          body += 'Created By: '+ newdoc.createdbyid+ '/n';
          body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
          body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
          mail.setHtmlBody(body);
    
          // Step 5. Add your email to the master list
          mails.add(mail);
    }
    
  // Step 6: Send all emails in the master list
   Messaging.SendEmailResult[] results = Messaging.sendEmail(mails);
 if (results[0].success) {
    System.debug('The email was sent successfully.');
 } else {
    System.debug('The email failed to send: '
          + results[0].errors[0].message);
 }
  
}


Thanks!
karthikeyan perumalkarthikeyan perumal
Hello  Aidel,

I Tested your code. its works well. and i got email when i create new Account. the only chnage i made is BODY part 
 
trigger NewFileAlert on Account (after insert) 
{
// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (Account newDoc : Trigger.new) 
  {
     // Step 1: Create a new Email
          Messaging.SingleEmailMessage mail = 
          new Messaging.SingleEmailMessage();
    
          // Step 2: Set list of people who should get the email
          List<String> sendTo = new List<String>();
          sendTo.add('rajam.karthikeyan@gmail.com');

          mail.setToAddresses(sendTo);
    
          // Step 3: Set who the email is sent from
          mail.setReplyTo('mailto.pkarthikeyan@gmail.com');
          mail.setSenderDisplayName('Aidel Bruck');
    
       
          // Step 4. Set email contents - you can use variables!
          mail.setSubject('New Document Added');
          String body = 'Please note: A new document has been added/n';
          mail.setHtmlBody(body);
    
          // Step 5. Add your email to the master list
          mails.add(mail);
    }
    
  // Step 6: Send all emails in the master list
   Messaging.SendEmailResult[] results = Messaging.sendEmail(mails);
 if (results[0].success) {
    System.debug('The email was sent successfully.');
 } else {
    System.debug('The email failed to send: '
          + results[0].errors[0].message);
 }
  
}
Below is the email which i got.. 
User-added image

kinldy check the body part which you added to this email content. hope this will give you some idea to debug. 

Thanks
karthik
 
Aidel BruckAidel Bruck
The trigger is on contentdocument, not account. 
I changed the body just like you did and I still did not get the email. 
Is there a problem to create an email alert through a trigger on content document?
Sitanshu TripathiSitanshu Tripathi
Hi Aidel Bruck,

Please once check the email Deliverability of your SF org. Maybe this will the reason that you can't get the mail.
Find from "Quick Find Box >> Deliverability (Type)".
Change the "Access Level" from "System Email Only" to "All Email".
User-added image
karthikeyan perumalkarthikeyan perumal
Hello Aidel, 

Yes, you are right.  i checked the same. trigger event will not work on ContentDocument. its based on ContentVersion, kinldy do the following.  

Delete Trigger in ContentDocument. Create same trigger in ContentVersion. its should work.  then try to upload file. your trigger will work 
 
trigger NewFileAlert on ContentVersion (after insert) 
{
// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
   
  for (ContentVersion  newDoc : Trigger.new) 
  {
   
     // Step 1: Create a new Email
          Messaging.SingleEmailMessage mail = 
          new Messaging.SingleEmailMessage();
    
          // Step 2: Set list of people who should get the email
          List<String> sendTo = new List<String>();
          sendTo.add('rajam.karthikeyan@gmail.com');

          mail.setToAddresses(sendTo);
    
          // Step 3: Set who the email is sent from
          mail.setReplyTo('mailto.pkarthikeyan@gmail.com');
          mail.setSenderDisplayName('Aidel Bruck');
    
       
          // Step 4. Set email contents - you can use variables!
          mail.setSubject('New Document Added');
          String body = 'Please note: A new document has been added/n';
          mail.setHtmlBody(body);
    
          // Step 5. Add your email to the master list
          mails.add(mail);
    }
    
  // Step 6: Send all emails in the master list
   Messaging.SendEmailResult[] results = Messaging.sendEmail(mails);
 if (results[0].success) {
    System.debug('The email was sent successfully.');
 } else {
    System.debug('The email failed to send: '
          + results[0].errors[0].message);
 }
  
}

I got a emai alert, 

for more information kinldy refer below thread. 

https://salesforce.stackexchange.com/questions/76015/trigger-on-contentdocument-not-working hope it solve your isssue. 


Thanks
karthik