Results 1 to 3 of 3

Thread: Sugar Email with attachments

  1. #1
    thamima is offline Senior Member
    Join Date
    Mar 2009
    Location
    Pretoria, South Africa
    Posts
    23

    Default Sugar Email with attachments

    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.

  2. #2
    Superman's Avatar
    Superman is offline Sugar Community Member
    Join Date
    Oct 2005
    Location
    Kazakhstan
    Posts
    852

    Default Re: Sugar Email with attachments

    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.

  3. #3
    Superman's Avatar
    Superman is offline Sugar Community Member
    Join Date
    Oct 2005
    Location
    Kazakhstan
    Posts
    852

    Default Re: Sugar Email with attachments

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Email Attachments
    By prasanthvs in forum Developer Help
    Replies: 0
    Last Post: 2008-04-19, 07:09 AM
  2. Replies: 3
    Last Post: 2007-12-28, 10:07 PM
  3. Can't see email attachments
    By m3freak in forum Help
    Replies: 0
    Last Post: 2006-07-18, 05:15 PM
  4. Email and Attachments
    By saint michel in forum Help
    Replies: 0
    Last Post: 2006-02-09, 02:14 PM
  5. Email Attachments
    By SpiicyTuna in forum Help
    Replies: 2
    Last Post: 2006-01-18, 06:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •