How to send email programmatically?

HI all,

I have a SUGARCRM PRO 7.6 installation working correctly with many custom modules. I integrated the platform with another web based financial software that send Sugar some data via a custom rest endpoint. When those data arrive in sugar I need to send an email to related contacts via custom scheduler. In the past I used a PHPMailer function but this one is deprecated so I'd like to use the new Mailer Factory but I can't find some documentation. Has someone already used this new feature and wrote some best practices? Thanks a lot for any information. I will try to set up this feature on my own and if I'll be able to do it I will post the code here.

  • Hi Federico,

    We have /Mail POST end point. Did you try to use this? Would it work for your use case? (It also exist in 7.6 to check navigate http://<yoursugar>/rest/v10/help in your browser and search for Mail)

    Best Regards

    Tevfik Tümer

    Developer Support Engineer

  • If you want to do it in a scheduled job, this has worked for me:

        $emailObj = new Email();
        $defaults = $emailObj->getSystemDefaultEmail();
        $mail = new SugarPHPMailer();
        $mail->From = $defaults['email'];
        $mail->FromName = $defaults['name'];
        $mail->Subject = 'Put the title here';
        $mail->Body = 'Put the body here';
        $mail->addAttachment('/location/of/file/in/filesystem/file.csv', "name-of-attached-file-in-the-email.csv", Encoding::Base64, "text/csv");

    If you want to do it in Sidecar (or in javascript in general) let me know and I can send you a more appropriate code snippet.

  • Hi all,

    hanks a lot for all your replies. I like a lot Tevfik Tümer response because It is more relevant for Sugar Pro Version 7.6. Rest endpoint for sending email is very interesting even if I don't want to make a Rest call via Curl programming a script inside Sugar Framework: using a paragon It seems to me to go out from the door and come back from the window... However I will use it for integrate Sugar with our website, tracking customer emails used in wordpress.
    I've already used and very well known Alan Apter solution with previously Sugar CE releases but in the newest versions of Sugar is deprecated.
    So let me post here my personal solution, found after a morning study of Emails sugar module. Record an email data in Sugar and send it to all recipients you want is more simply that it seems.

    Simply create a new Bean for Email module with all the relevant information and save it. Then, after this, simpy call save() method.
    Here is my code:



    class custom_email_sender {

         private $mail_from = '';

         private $mail_from_name = 'Info example";

         public function send_emails($args){

              foreach($args as $single_mail){

                $email = BeanFactory::newBean('Emails');

                $email->from_addr = $this->mail_from;

                $email->from_name = $this->mail_from_name;


                $email->reply_to_name = $this->mail_from_name;

                $email->reply_to_email = $this->mailfr_om;

                $email->name = $single_mail['email_subject'];

                $email->description_html = $body_email;

                $email->to_addrs_arr = $single_mail['email_to'];

                $email->parent_type = 'Contacts';

                $email->parent_id = $single_mail['contact_id'];







    $args variable is an array containing whatever you need to pass to the function email and so you could customize any of the relevant information of the email both in sugar bean and in smtp message. The only thing to particularly care is the format of the data passed to the property "to_addrs_arr" because the send() method use a foreach to add recipients that need the following format:


         'email_to' =>


                                       'email' => '',

                                       'display' => 'Info Example'


                                       'email' => '',

                                       'display' => 'Info2 Example'

    and so on.

    Thanks a lot again to all contributors.



  • How does one handle email attachments via this bean?


  • In the Email Class declared on modules/Emails/Email.php file you could find several function to manage attachments. I've had no time to search deep within the methods but you could find for sure a simply way to upload and attach a file, both using Sugar Documents, Sugar Notes or custom uploads.

    If I could I will post here in the next future a solution.

  • Hai Alan Apter

    I have to add attachment in custom module in after save logic hook code.. is this work in aftersave logic hook and in this code  $mail->addAttachment('/location/of/file/in/filesystem/file.csv'"name-of-attached-file-in-the-email.csv", Encoding::Base64, "text/csv");  

    added file name and Extension to add those dynamically ..