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
satakshisatakshi 

Sending a mail using apex trigger

hello i am writting a trigger to send mail. i am having two objects one is custom and one is contact. on custom object there is lookup relationship with contact. means from custom object we can choose contact. contact will be having email as a mandatory field. when i will choose contact from custom object(Sent feedback) using lookup field then mail will be sent to email address of that contact. can you help me with this?? this is my code. i am not getting mail.

trigger SentFbtrigger on Sent_Feedback__c (after insert) {
    
//Create a master list to hold the emails we'll send

   List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for(Sent_Feedback__c s : Trigger.new) {
    if(s.Contact__r.Email != null && s.Contact__r.Name != null) {
       
        //Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      //Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(s.Contact__r.Email);
      mail.setToAddresses(sendTo);
        mail.setTargetObjectId(s.Contact__r.Id);
        
         //Set who the email is sent from
       mail.setReplyTo('james12@ror.com');
      mail.setSenderDisplayName('James R');
    
    
      // Set email contents - you can use variables!
      mail.setSubject('Subject Content');
      String body = 'Dear ' + s.Contact__r.FirstName + ', ';
      body += 'Email Body';
      
      mail.setHtmlBody(body);
    
      //Add your email to the master list
      mails.add(mail);
    }
  }
  // Send all emails in the master list
  Messaging.sendEmail(mails);
}
Best Answer chosen by satakshi
satakshisatakshi
this is the correct code:

trigger SendEmail on Sent_Feedback__c (after insert) {
   
        set<id> ContactId = new set<id>();
 
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
  for (Sent_Feedback__c myContact : Trigger.new) {
    
    if (myContact.Contact__c != null )
    {
          ContactId.add(myContact.Contact__c);
    }
         Contact con = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :ContactId];
      // 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(con.email);
      mail.setToAddresses(sendTo);
   
      // Step 3: Set who the email is sent from
      mail.setReplyTo('utkarsha.up10@gmail.com');
      mail.setSenderDisplayName('Utz patil');
   
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('puja.patil@aress.com');
      mail.setCcAddresses(ccTo);
 
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + con.FirstName;
     
      mail.setHtmlBody(body);
   
      // Step 5. Add your email to the master list
      mails.add(mail);
        Messaging.sendEmail(mails);
    }
  }
  // Step 6: Send all emails in the master list

All Answers

Uvais KomathUvais Komath
Make that email an asynchronous call to a class!
for this ,Take the logic outside the trigger and write it on an apex class. Now call this class from trigger. Checkout trailhead for asynchronous apex module

satakshisatakshi
its working now but email is going in spam. what to do now?
Uvais KomathUvais Komath
Setup > email administration > deliverability
Then enable email security compliance
User-added image
satakshisatakshi
hey thank you Uvais Komath .. its working 😁
satakshisatakshi
this is the correct code:

trigger SendEmail on Sent_Feedback__c (after insert) {
   
        set<id> ContactId = new set<id>();
 
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
  for (Sent_Feedback__c myContact : Trigger.new) {
    
    if (myContact.Contact__c != null )
    {
          ContactId.add(myContact.Contact__c);
    }
         Contact con = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :ContactId];
      // 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(con.email);
      mail.setToAddresses(sendTo);
   
      // Step 3: Set who the email is sent from
      mail.setReplyTo('utkarsha.up10@gmail.com');
      mail.setSenderDisplayName('Utz patil');
   
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('puja.patil@aress.com');
      mail.setCcAddresses(ccTo);
 
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + con.FirstName;
     
      mail.setHtmlBody(body);
   
      // Step 5. Add your email to the master list
      mails.add(mail);
        Messaging.sendEmail(mails);
    }
  }
  // Step 6: Send all emails in the master list
This was selected as the best answer
Uvais KomathUvais Komath
Great! U can mark the question as solved then!
Noah DiPasqualeNoah DiPasquale
How many Emails can be sent using above format, can i also download the email in the form of a word document in plase of sending the email.
VENKATA KRISHNA MOHANVENKATA KRISHNA MOHAN
Write a trigger on contact such that when ever a contact record is created u should update the email \
sales manager 983sales manager 983
hi
i want help from you guys to create trigger to send email 
scenario is number of tickets available  field is a formula field when ever the the  number of tickets avialbale  field is zero email should be sent to the event manager
i tried to do it  but trigger not supporting  the formula filed 
if anyone have the solution please let  me know