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
kzmpkzmp 

How to send email through gmail from a Trigger

Hi,
I have a requirement to be able to send emails from a trigger through gmail. Does anybody know how to do that?
I have already setup Email sending through Gmail, however one of the limitations is that Workflows and Apex triggers send emails from Salesforce and not through gmail.

Thanks!
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
There is no way to have Salesforce use an alternate SMTP server to send mail.  You can set the Reply To address to a GMail address, though.  At least the replies will go to the GMail address.  Does this satisfy your requirement?

See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_base.htm?search_text=setreplyto
Raj VakatiRaj Vakati


Use Organization-Wide Email Addresses

If your Salesforce org requires users to share a common email alias, you can define a list of organization-wide addresses for each user profile. When sending email from Salesforce, users with these profiles can select their own email address or the organization-wide email address for the email’s From field. Replies are delivered to the selected address.

https://help.salesforce.com/articleView?id=orgwide_email.htm&type=5

https://help.salesforce.com/articleView?id=emailadmin_manage_orgwide_email_addresses.htm&type=5
 
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'doNotReply@blahblah.com'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if ( owea.size() > 0 ) {
    mail.setOrgWideEmailAddressId(owea.get(0).Id);
}

 
kzmpkzmp
Raj and Glyn, thank you so much for your responses. 
We do not want to send the email through Salesforce because users get this weird "sent via ..." details and because the email was sent from Salesforce on behalf of my company's domain spam filters may catch it cause troubles. 
That is why I have setup the Send through Gmail option for sending emails in Salesforce however that options has the limitations that emails sent from triggers and workflows are still sent from Salesforce. 

Is there a way to make emails sent from triggers to be sent through gmail as well? What if I setup emai relay instead of send through gmail?