Email Template With Attachment – SugarCRM / SuiteCRM

If user want email with attachment this can be done very easily in SugarCRM / SuiteCRM. Steps are as below,

Step 1 : Get the Email Template detail using following code,

$emailTemp = new EmailTemplate();
$emailTemp->disable_row_level_security = true;

Step 2 : Get the Attachments of Email Template using following code,

$note = new Note();
$where = “notes.parent_id =’$emailTemplateId'”;
$attach_list = $note->get_full_list(“”, $where, true);

Step 3 : Sending Email Code using following code,

$html=”Addtional text”;

$mail = new SugarPHPMailer();

$mail->From = $FromEmailAddress;
$mail->FromName = $FromEmailAddress;
$mail->Subject = from_html($emailTemp->subject);
$mail->AddAddress(“sendingmailid”, “name”);
$mail->AddAddress(“sendingmailid2”, “name2”);

$mail->Body_html = from_html($emailTemp->body_html);
$mail->Body = wordwrap($emailTemp->body_html, 900);
// Add attachemts to the Email Start
$attachments = array();
$attachments = array_merge($attachments, $attach_list);
foreach ($attachments as $attached) {
$filename = $attached->filename;
$file_location = ‘upload/’ . $attached->id;
$mime_type = $attached->file_mime_type;
$mail->AddAttachment($file_location, $filename, ‘base64’, $mime_type); //Attach each file to message
// Add attachemts to the Email End
$mail->IsHTML(true); //Omit or comment out this line if plain text

//Send the message, log if error occurs
if (!$mail->Send()) {
$GLOBALS[‘log’]->fatal(‘ERROR: Message Send Failed’);

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at

Thank you.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *