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,

require_once(‘modules/EmailTemplates/EmailTemplate.php’);
$emailTemp = new EmailTemplate();
$emailTemp->disable_row_level_security = true;
$emailTemp->retrieve($emailTemplateId);
print_r($emailTemp);

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

require_once(‘modules/EmailTemplates/EmailTemplate.php’);
require_once(‘modules/Notes/Note.php’);
$note = new Note();
$where = “notes.parent_id =’$emailTemplateId'”;
$attach_list = $note->get_full_list(“”, $where, true);
print_r($attach_list);

Step 3 : Sending Email Code using following code,

$html=”Addtional text”;

require_once(‘include/SugarPHPMailer.php’);
$mail = new SugarPHPMailer();
$mail->ClearAllRecipients();
$mail->ClearReplyTos();

$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);
$mail->Body.=$html;
// 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
$mail->prepForOutbound();
$mail->setMailerForSystem();

//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 info@infotechbuddies.com

Thank you.

Spread the love

Leave a Reply

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