Skip to content

Instantly share code, notes, and snippets.

@dmulvi
Last active August 29, 2015 14:23
Show Gist options
  • Save dmulvi/df9c9e2a59ca9efb05f1 to your computer and use it in GitHub Desktop.
Save dmulvi/df9c9e2a59ca9efb05f1 to your computer and use it in GitHub Desktop.
SugarCRM - Email w/ Attachment in Code
<?php
require_once('modules/Emails/Email.php');
require_once('include/SugarPHPMailer.php');
$emailObj = new Email();
$mail = new SugarPHPMailer();
// Create an id for the email
$emailObj->id = create_guid();
$emailObj->new_with_id = true;
$emailObj->parent_type = 'Accounts';
$emailObj->parent_id = $account['id'];
$emailObj->status = 'whatever';
$emailObj->assigned_user_id = $user->id;
// Clear recipients
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
// Get the email addresses and names
$currentUser_email = $user->email1;
$currentUser_name = $user->first_name.' '.$user->last_name;
$currentAccount_email = $account['email1'];
$currentAccount_name = $account['name'];
// Set the different fields for the attachments
$file_name = $docRevision->filename;
$location = getcwd()."/upload/{$docRevision->id}";
$mime_type = $docRevision->file_mime_type;
// Add attachment to email
$mail->AddAttachment($location, $file_name, 'base64', $mime_type);
// load email template
$template = BeanFactory::getBean('EmailTemplates', '534bcab4-fc6e-6d04-8831-5543bd2a9e0f');
// Set the typical fields in the email
$mail->From = $currentUser_email;
$mail->FromName = $currentUser_name;
$mail->Subject = $template->subject;
$mail->Body = from_html($template->body);
$mail->AddAddress($currentAccount_email, $currentAccount_name);
$mail->IsHTML( true );
$emailObj->save();
$mail->prepForOutbound();
$mail->setMailerForSystem();
$mail->send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment