+ Start a Discussion
Chitral ChaddaChitral Chadda 

How to keep track of html email sent through code

Is there any workaroud to check that email is opened or not, no. of times openen etc for emails sent through code.??
Bcz HTML email status reports builts reports for emails based on html email template  .

can we track all those html email which are sent through code ?

Best Answer chosen by Chitral Chadda
Chitral ChaddaChitral Chadda

heelo balaji, thankyou tons !!
If on account , type agriculture is selected for industry then on contact insertion it should send email .
I was able to send email before using  :

mail.setTargetObjectId(a.id);
mail.setSaveAsActivity(true);
but now i get error
 

trigger definename on Contact (after insert )
 
 {  
 
   set<id> aId = new set<id>();
   if(trigger.isInsert)
   for (contact c :trigger.new)
   {
         
      {
       aId.add(c.AccountId);
      }
    }
  
  
  for(account  a : [select id,Industry from account where id IN : aId  ])
  {
     if (a.Industry=='agriculture')
     {
     
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     mail.setToAddresses(new String[] {'cse10315.sbit@gmail.com'});
     mail.setSubject('hi');
     mail.setHtmlBody('Congratulations !!!');
     mail.setTargetObjectId(a.id);
     mail.setSaveAsActivity(true);
     Messaging.sendEmail( new Messaging.SingleEmailMessage[]{mail});
     }
  
  }   
  
}
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger definename caused an unexpected exception, contact your administrator: definename: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only Users, Contact or Lead allowed for targetObjectId : 0019000001BFdA0.: []: Trigger.definename: line 27, column 1


I get this error also i cant find the realted list of HTML email to add on the account page layout ?

All Answers

BalajiRanganathanBalajiRanganathan
You have set mail.setSaveAsActivity(true); replace mail with your vairiable for Messaging.SingleEmailMessage.

also while using this you need to have set mail.setTargetObjectId(), The ID of the contact, lead, or user to which the email will be sent
Chitral ChaddaChitral Chadda
Hello Can u tell lil more about this "set mail.setSaveAsActivity(true); replace mail with your vairiable for Messaging.SingleEmailMessage." Means if i tk variable as sendm Messaging.SingleEmailMessage send m So i write Sendm.setSaveAsActivity(true); // set to true So this will show the status in activity section ? Would it display whther mail opened or not ( checkbox ) , no. Of times mail opened.
BalajiRanganathanBalajiRanganathan
yes you got it. it is Sendm.setSaveAsActivity(true); in your case.

refer the link below to see where you can see the status and how it tracks and limitations.

https://help.salesforce.com/HTViewHelpDoc?id=email_track.htm&language=en_US
https://help.salesforce.com/apex/HTViewSolution?id=000006731&language=en_US
Chitral ChaddaChitral Chadda
I went through both of these documents bt still very unsure where it wud display the all these status 1. whether i will hav to generate html email report ( if so wud it show email status which we sent through code ) ?? Or 2. Sone othere place where it will show the the track of email sent
BalajiRanganathanBalajiRanganathan
you can do report as well you can go to the contact or lead or user record to whom the email is sent (
mail.setTargetObjectId()) and you can see the status as a related list.

the belwo is from the first link

Detail pages for contacts, leads, and person accounts include an HTML Email Status related list. Each email you send to a person or mass distribution list is listed, including the date the email was sent, first opened, last opened, and the total number of times it was opened by all users (multiple openings by the same user are counted)
Chitral ChaddaChitral Chadda
Alright means i can check the status of enail sent through code in both ways 1. By building the reports 2. Individually on particular records as mentioned in docs I vl test it . Nd yes , mail.setTargetObjectId() ?? This wuld contain id of the object or something else?
BalajiRanganathanBalajiRanganathan
Contact c = [select id from Contact Limit 1];
mail.setTargetObjectId(c.id); //The ID of the contact, lead, or user to which the email will be sent
Chitral ChaddaChitral Chadda

heelo balaji, thankyou tons !!
If on account , type agriculture is selected for industry then on contact insertion it should send email .
I was able to send email before using  :

mail.setTargetObjectId(a.id);
mail.setSaveAsActivity(true);
but now i get error
 

trigger definename on Contact (after insert )
 
 {  
 
   set<id> aId = new set<id>();
   if(trigger.isInsert)
   for (contact c :trigger.new)
   {
         
      {
       aId.add(c.AccountId);
      }
    }
  
  
  for(account  a : [select id,Industry from account where id IN : aId  ])
  {
     if (a.Industry=='agriculture')
     {
     
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     mail.setToAddresses(new String[] {'cse10315.sbit@gmail.com'});
     mail.setSubject('hi');
     mail.setHtmlBody('Congratulations !!!');
     mail.setTargetObjectId(a.id);
     mail.setSaveAsActivity(true);
     Messaging.sendEmail( new Messaging.SingleEmailMessage[]{mail});
     }
  
  }   
  
}
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger definename caused an unexpected exception, contact your administrator: definename: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only Users, Contact or Lead allowed for targetObjectId : 0019000001BFdA0.: []: Trigger.definename: line 27, column 1


I get this error also i cant find the realted list of HTML email to add on the account page layout ?
This was selected as the best answer
BalajiRanganathanBalajiRanganathan
you cannot set accountid for targetObjectId. it should be id of contact, lead or user
mail.setTargetObjectId(c.id); //The ID of the contact, lead, or user to which the email will be sent
Chitral ChaddaChitral Chadda
Thanx  balaji related to this  1 face one problem ...
 
trigger definename on Contact (after insert )
 
 {  
 
   set<id> aId = new set<id>();
   if(trigger.isInsert)
   for (contact c :trigger.new)
   {
         
      {
       aId.add(c.Id);
      }
    }
  
  
  for(contact  a : [select id,LeadSource,Email from contact where id IN : aId  ])
  {
     if (a.LeadSource=='Web')
     {
     
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     List<String> sendTo = new List<String>();
     sendTo.add(a.Email);
     mail.setToAddresses(sendTo);
     mail.setSubject('hi');
     mail.setHtmlBody(' Hello Chadda !! <image src="https://chitral-dev-ed--c.ap1.content.force.com/servlet/servlet.ImageServer?id=01590000006MdbA&oid=00D90000000vuPf&lastMod=1418471178000" ');
     mail.setTargetObjectId(a.id);
     mail.setSaveAsActivity(true);
     Messaging.sendEmail( new Messaging.SingleEmailMessage[]{mail});
     }
  
  }   
  
}



When i send the  image in the htmlbody using img tag , i have stored the image in document .
but the HTML email status (related list ) dosent show the count of email opened ? 

i have checked "externally available use" checkbox on document also .. 
HTML email status showing  0 ,#times opened even though i have opened the mail .
1 more thing  i am able to see that image from my cell phone but , when i login desktop  i cant see any image there in the mail .
i cudnt find any help on the net.
Chitral ChaddaChitral Chadda
for others :
use img not mage
mail.setHtmlBody(' Hello Chadda !! <img src="https://chitral-dev-ed--c.ap1.content.force.com/servlet/servlet.ImageServer?id=01590000006MdbA&oid=00D90000000vuPf&lastMod=1418471178000" ');