HOWTO: Add and retrieve email addresses in a module thru code

sugarcrmdevelopers —  March 8, 2011 — 9 Comments

I came across this excellent post from the forums, where poster BlackMumba detailed how to add and retrieve the associated email records for a record. Inspired by the post,  I’ve brought his solution here and expanded upon it to show other use cases. Here’s how you can do it.

To add a new email address, you can do the following:

If you want to retrieve the primary email address for a record, you can do this:

And to grab all the email addresses for a record, use this code:

9 responses to HOWTO: Add and retrieve email addresses in a module thru code


    Great tip – thank you! Love the examples please keep them coming!


    Great tip! Keep em coming!


    I am doing something a bit different. I am trying to write a job that will be Scheduled to run once a day, to e-mail overdue Tasks to their assgined users. Is there a way I can still use the getPrimaryAdress method?


    If the email address already exists, will it not save it?  I’m trying to copy an email address in a before_save logic from a contact to a sub-module (don’t ask – long story). The above code produces no saved email address on the sub-module


    How should I get Module and ID for a specific email address (using the bean)?

    function whereIsThisEmailFrom($email){
    …. wtf code ??? ….
    if( not_found_condition) return array(0,0);
    return array(‘id’,’module’);


    I’ve been trying to use these methods to gather all the email addresses from a contact, update information in the contact and save it (which then breaks the email address relationships) then add the email addresses back to the contact after the fact.

    I’ve got this mostly working but I can’t get any of the email addresses to save as the primary address.

    >addAddress($primaryemailaddress, true);

    Doesn’t appear to store the address as the Primary any longer (in version Has anyone else ran into this?


    What is the best way to un-relate an address in code. E.g. remove the relationship between a given address id and a given contact id?


    This solution of adding an email address will just overwrite other existing emails in that bean. Please update it to work just for adding an email address in the list.

Trackbacks and Pingbacks:

  1. More Email Maniuplation with Code - October 13, 2011

    […] time ago, John Mertic (SugarCRM Community Manager and ace developer) posted up a nice tutorial on retrieving and using email addresses within Sugar via PHP. I’ve found that posting pretty helpful, but I needed to go a few more […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s