Please help
I need help, I have a custom module with a button that fires custom code. As part of this code I want to pick up a document (pdf/zip), attach it to Email Template and sent it to email address.![]()
Please help
I need help, I have a custom module with a button that fires custom code. As part of this code I want to pick up a document (pdf/zip), attach it to Email Template and sent it to email address.![]()
You need to create Note with attachment, then relate this note to created email (with status=draft), and then just redirect user to Compose email (indicating email_id) screen.
Farkhad Rakhimzhanov
E-mail: farkhad@gmail.com
Skype: rakikama
SuperTimesheet and Invoicing — timesheet tool with invoicing for SugarCRM.
Book time against Cases, Project Tasks and Projects.
Create invoice regarding booked time, print it in PDF or HTML,
customize template as you like.
Sample code:
PHP Code:function composeEmail($focus, $document) {
global $log;
global $mod_strings;
global $current_user;
require_once('modules/Emails/Email.php');
require_once('modules/Notes/Note.php');
//First Create e-mail draft
$email_object = new Email();
// set the id for relationships
$email_object->id = create_guid();
$email_object->new_with_id = true;
//subject
$email_object->name = $mod_strings['LBL_EMAIL_CONTRACT_FOR'].$focus->name."";
//body
$email_object->description_html = $mod_strings['LBL_EMAIL_DEFAULT_DESCRIPTION'];
//parent type, id
//$email_object->parent_type = "Quotes";
//$email_object->parent_id = $focus->id;
//type is draft
$email_object->type = "draft";
$email_object->status = "draft";
// link the sent pdf to the relevant account
if(!empty($focus->company_id)) {
$email_object->load_relationship('accounts');
$email_object->accounts->add($focus->company_id);
}
//check to see if there is a client contact associated with this contract
if(!empty($focus->clientcontact_id)) {
global $beanFiles;
require_once($beanFiles['Contact']);
$contact = new Contact;
$contact->retrieve($focus->clientcontact_id);
if(!empty($contact->email1) || !empty($contact->email2)) {
//contact email is set
$email_object->to_addrs_ids = $focus->clientcontact_id;
$email_object->to_addrs_names = $focus->clientcontact.";";
if(!empty($contact->email1)){
$email_object->to_addrs_emails = $contact->email1.";";
$email_object->to_addrs = $focus->clientcontact." <".$contact->email1.">";
} elseif(!empty($contact->email2)){
$email_object->to_addrs_emails = $contact->email2.";";
$email_object->to_addrs = $focus->clientcontact." <".$contact->email2.">";
}
// create relationship b/t the email(w/pdf) and the contact
$contact->load_relationship('emails');
$contact->emails->add($email_object->id);
}//end if contact name is set
}
elseif(!empty($focus->company_id)) {
require_once('modules/Accounts/Account.php');
$acct = new Account();
$acct->retrieve($focus->copmany_id);
if(!empty($acct->email1) || !empty($acct->email2)) {
//acct email is set
$email_object->to_addrs_ids = $focus->company_id;
$email_object->to_addrs_names = $focus->company_name.";";
if(!empty($acct->email1)){
$email_object->to_addrs_emails = $acct->email1.";";
$email_object->to_addrs = $focus->company." <".$acct->email1.">";
} elseif(!empty($acct->email2)){
$email_object->to_addrs_emails = $acct->email2.";";
$email_object->to_addrs = $focus->company." <".$acct->email2.">";
}
// create relationship b/t the email(w/pdf) and the acct
$acct->load_relationship('emails');
$acct->emails->add($email_object->id);
}//end if acct name is set
}
//assigned_user_id
$email_object->assigned_user_id = $current_user->id;
//Save the email object
global $timedate;
$email_object->date_start = $timedate->to_display_date_time(gmdate($GLOBALS['timedate']->get_db_date_time_format()));
$email_object->save(FALSE);
$email_id = $email_object->id;
//Handle PDF Attachment
$revision = new DocumentRevision();
$revision->retrieve($document->document_revision_id);
$file_name = $focus->name.'_'.$focus->svnumber.'.'.$revision->revision.'.pdf';
$note = new Note();
$note->filename = $file_name;
$note->team_id = "";
$note->file_mime_type = "application/pdf";
$note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].$file_name;
//save the pdf attachment note
$note->parent_id = $email_object->id;
$note->parent_type = "Emails";
$note->save();
$note_id = $note->id;
$source = $GLOBALS['sugar_config']['upload_dir']."/".$document->document_revision_id;// "cache/upload/".$file_name;
$destination = "cache/upload/".$note_id;
if (!copy($source, $destination)){
$msg = str_replace('$destination', $destination, $mod_strings['LBL_COPY_ERROR']);
die($msg);
}
//return the email id
return $email_id;
}
Farkhad Rakhimzhanov
E-mail: farkhad@gmail.com
Skype: rakikama
SuperTimesheet and Invoicing — timesheet tool with invoicing for SugarCRM.
Book time against Cases, Project Tasks and Projects.
Create invoice regarding booked time, print it in PDF or HTML,
customize template as you like.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks