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.

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

        require_once('include/SugarPHPMailer.php');
        $emailObj = new Email();
        $defaults = $emailObj->getSystemDefaultEmail();
        $mail = new SugarPHPMailer();
        $mail->setMailerForSystem();
        $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");
        $mail->prepForOutbound();
        $mail->AddAddress('test@test.com');
        @$mail->Send();
    

    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:

    <code>

    <?php

    class custom_email_sender {

         private $mail_from = 'info@example.com';

         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'];

                $email->save();

                $email->send();

              }

         }

    }

    </code>

    $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:

    array(

         'email_to' =>

                                  [0]=>

                                       'email' => 'info@example.com',

                                       'display' => 'Info Example'

                                  [1]=>

                                       'email' => 'info2@example.com',

                                       'display' => 'Info2 Example'

    and so on.


    Thanks a lot again to all contributors.

    Bye

    Federico

Reply
  • 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:

    <code>

    <?php

    class custom_email_sender {

         private $mail_from = 'info@example.com';

         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'];

                $email->save();

                $email->send();

              }

         }

    }

    </code>

    $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:

    array(

         'email_to' =>

                                  [0]=>

                                       'email' => 'info@example.com',

                                       'display' => 'Info Example'

                                  [1]=>

                                       'email' => 'info2@example.com',

                                       'display' => 'Info2 Example'

    and so on.


    Thanks a lot again to all contributors.

    Bye

    Federico

Children