AnsweredAssumed Answered

How do you Add Attachments to an Email Object in PHP?

Question asked by H D on Jun 20, 2014
Latest reply on Jul 10, 2014 by H D
Hi,

I was wondering if there was a way to add an attachment to an email object. I have currently written a program that either sends an email or gives the user the option to store it as a draft. I am able to send the email with the attachments just fine (I used SugarPHPMailer() to do this), but when I save it as a draft, there are no attachments.

Does anyone know how to attach something to an email object (so Email(), not SugarPHPMailer())?

My code for the email is this:
require_once('modules/Emails/Email.php');
require_once('include/SugarPHPMailer.php');
$emailObj  = new Email();
$mail   = new SugarPHPMailer();

$mail->ClearAllRecipients();
$mail->ClearReplyTos();

$currentUser_email  = getEmailAddress( $f_currentUser );
$currentUser_name  = getUserName( $f_currentUser );
$currentAccount_email = getEmailAddress( $f_currentAccount );
$currentAccount_name = getFullName( $f_currentAccount );

$mail->From  = $currentUser_email;
$mail->FromName = $currentUser_name;
$mail->AddAddress( $currentAccount_email, $currentAccount_name );
$mail->Subject  = $f_subject;
$mail->Body = from_html( $f_body );
$mail->IsHTML(true);

// Get the email template id and the attachments associated to it
$template_id = $f_template->id;
include_once( 'module/Notes/Note.php' );
$note        = new Note();
$where       = "notes.parent_id = '$template_id'";

// Get full list gets all attachments of all notes related to the record
$attachments = $note->get_full_list( "", $where, true );
$all_attachments = array();
$all_attachments = array_merge( $all_attachments, $attachments );

// Loop through record
foreach( $all_attachments as $attachment )
{
  // Set the different fields for the attachments
  $file_name  = $attachment->filename;
  $location  = "upload/{$attachment->id}";
  $mime_type  = $attachment->file_mime_type;

  // Add attachment to email
  $mail->AddAttachment($location, $file_name, 'base64', $mime_type);
}

// If the user chose to edit the email before sending it
if ( $edit_email_before_sending == true )
{
  // Will be stored in the client's account section
  $emailObj->to_addrs   = $currentAccount_email;
  $emailObj->type   = 'draft';
  $emailObj->deleted   = '0';
  $emailObj->name   = $mail->Subject;
  $emailObj->description   = null;
  $emailObj->description_html     = $mail->Body;
  $emailObj->from_addr  = $currentUser_email;
  $emailObj->date_sent   = TimeDate::getInstance()->nowDb();
  $emailObj->status               = 'draft';
  $emailObj->save();
}
// Send the email if the user does not want to edit it before hand
else
{
  $mail->prepForOutbound();
  $mail->setMailerForSystem();
  $mail->send();
}
I have tried using the variable 'attachments' listed under line 122
https://github.com/sugarcrm/sugarcrm_dev/blob/master/modules/Emails/Email.php#L122
So, something similar to:
$emailObj->attachments

But again, no luck. Would someone be able to point me in the right direction?

Thanks!

Outcomes