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.

  • 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 Manually.how to add those dynamically ..

Reply Children
No Data