Pages

Thursday, April 12, 2012

SugarCRM Customization: Email Templates with Attachments

Earlier posts on this blog have described the manner in which one can send email messages via SugarCRM, with one example going as far as demonstrating the manner in which one can use an email template in the process.

In the case of the latter, people often times wonder if it is possible to include attachments with the message. It certainly is possible and that is focus of this post. For the purposes of our example, we will assume that we are altering the above referenced example that uses a template.

Enhancing the code to accommodate attachments is actually quite easy. But before we can apply that modification we must attach the file to the template within SugarCRM.

The process for attaching it is no different than it would be for any other email template. Simply edit the email template and then attach the file. Take note of the ID value corresponding to the email template as we will need it within our code. Before we get to that point, let us take a look at what else happened when the email template was saved.

Navigate to the Notes module and notice that the file you attached to the email template has been added as a note. The entry usually has a Subject line that begins with the text "Email Attachment." This tells us two things: one, an attachment is actually a note entry related to an email template, and two, the actual file that represents the attachment is stored in the cache/upload folder. Both of these tidbits will be important to our process.

Let us begin with our modifications.

In the previous example that utilizes email templates, you will find the following code:


$mail->IsHTML(true); //Omit or comment out this line if plain text
$mail->prepForOutbound();
$mail->setMailerForSystem();

Modify the above so it looks as follows:


$mail->IsHTML(true); //Omit or comment out this line if plain text

//Attachments
require_once('modules/Notes/Note.php');
$note = new Note();
$where = "notes.parent_id = '$email_template_id' "; 
$attach_list = $note->get_full_list("", $where, true); //Get all Notes entries associated with email template
$attachments = array();
$attachments = array_merge($attachments, $attach_list);
foreach ($attachments as $attached)
{
$filename = $attached->filename;
$file_location = 'cache/upload/' . $attached->id;
$mime_type = $attached->file_mime_type;
$mail->AddAttachment($file_location, $filename, 'base64', $mime_type); //Attach each file to message
}
//End

$mail->prepForOutbound();
$mail->setMailerForSystem();

What is the code doing?

It is essentially retrieving all the notes entries that are linked to the specific email template in question. Secondly, once the list is retrieved, the code iterates through each entry to grab the file name and physically attach it to the email message that is about to be sent.

That is it! 



2 comments:

  1. great tutorial, just one question, if i'm gonna build the same thing for mssql i need to build the where clause separately. so is there any sugar way to build the where clause as i want sugar to handle building where clause.

    ReplyDelete
    Replies
    1. I don't understand why you would need to do that.

      Delete

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.