HOWTO: Send and archive an email in Sugar via PHP

sugarcrmdevelopers —  August 15, 2011 — 12 Comments

One of the big mysteries I’ve seen Sugar developers run into is trying to send an email out from code, for example from a logic hook. I saw this post on the forums last week and I thought I would whip up a quick function that could do this. In addition, it provides the ability to archive the email inside Sugar itself, and optionally relate it to an existing record in the system. Here’s the code:

<?php
/**
 * Sends an email and archives it inside Sugar
 *
 * @param string $emailTo
 * @param string $emailSubject
 * @param string $emailBody
 * @param object SugarBean of record to related email to ( optional )
 */
function sendEmail($emailTo, $emailSubject, $emailBody, SugarBean $relatedBean = null)
{
    $emailObj = new Email();
    $defaults = $emailObj->getSystemDefaultEmail();
    $mail = new SugarPHPMailer();
    $mail->setMailerForSystem();
    $mail->From = $defaults['email'];
    $mail->FromName = $defaults['name'];
    $mail->ClearAllRecipients();
    $mail->ClearReplyTos();
    $mail->Subject=from_html($emailSubject);
    $mail->Body=from_html($emailBody);
    $mail->prepForOutbound();
    $mail->AddAddress($emailTo);

    //now create email
    if (@$mail->Send()) {
        $emailObj->to_addrs= '';
        $emailObj->type= 'archived';
        $emailObj->deleted = '0';
        $emailObj->name = $mail->Subject ;
        $emailObj->description = $mail->Body;
        $emailObj->description_html = null;
        $emailObj->from_addr = $mail->From;
        if ( $relatedBean instanceOf SugarBean && !empty($relatedBean->id) ) {
            $emailObj->parent_type = $relatedBean->module_dir;
            $emailObj->parent_id = $relatedBean->id;
        }
        $emailObj->date_sent = TimeDate::getInstance()->nowDb();
        $emailObj->modified_user_id = '1';
        $emailObj->created_by = '1';
        $emailObj->status = 'sent';
        $emailObj->save();
    }
}

Let me know if this helps other developers running into this problem and if there are any other changes that could be made to make it better.

12 responses to HOWTO: Send and archive an email in Sugar via PHP

  1. 

    Hi..thanks for shearing! I have post a contribution about Python integration with SugarCRM and I would like to share it here…is that possible? Here the link http://blog.hash-tag.com.ar/post/9005343992/python-sugarcrm-interaction

  2. 

    Typo in the if statement, missing opening bracket.

    if ( $relatedBean instanceOf SugarBean && !empty($relatedBean->id) ) {

  3. 

    $emailObj automatically overwritten by save() in htdocs/modules/emails/email.php with current user id.

  4. 

    For HTML emails with plaintext counter-parts, use (I’m calling these from an email template object $template)

    $mail->Body=$template->body_html;
    $mail->AltBody = $template->body;

    and on the email object,

    $emailObj->description = $mail->AltBody;
    $emailObj->description_html = $mail->Body;

  5. 
    saigonleasing.com November 13, 2012 at 3:46 am

    thk much…

  6. 

    What if, if we have to send to multiple recipients?

Trackbacks and Pingbacks:

  1. Where does History Panel data populate from? - SugarCRM Forums - November 2, 2011

    […] […]

  2. Accessing a PDF Template - March 27, 2012

    […] […]

  3. contacts + hooks + email + email templates + Save email in History - August 27, 2012

    […] […]

  4. Sugar Crm SMTP settings - November 29, 2012

    […] […]

  5. How do I notify all users when a new Lead is created? - March 25, 2013

    […] […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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