Send Mail in a job

Hi,

I work with SugarCRM pro 7.6 and I want to send a mail in one of my scheduled jobs.

I have tried to search a full example which explains how to send a mail in PHP but i can't find it.

I try the solutions in this post: https://community.sugarcrm.com/thread/28095?q=how%20to%20send%20email

But I always have errors in the log:

1-

09/07/16 13:45:08 [7536][1][FATAL] SMTP -> ERROR: Failed to connect to server. Code: 351289610 Reply: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
09/07/16 13:45:08 [7536][1][FATAL] MailerException - @(SmtpMailer.php:166 [6]) - Failed to connect to outbound SMTP Mail Server

2-

09/07/16 14:54:54 [15560][1][FATAL] SMTP -> ERROR: Failed to connect to server. Code: 49982720 Reply: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
09/07/16 14:54:54 [15560][1][FATAL] SugarPHPMailer encountered an error: SMTP Connect() failed.

PHPmailer is deprecated for 7.6 but I try it anyway, for this reason I want to use the REST /Mail service but I can't use it.

In the php.ini the ssl is enabled and if I try to send an email with the button 'Send test mail', I receive correctly the mail.

Please help me.

Best regards 

Stefano

Parents
  • Where did you read that PHPmailer is deprecated for 7.6? Are you sure that's the case?

    In any case, can I see the code you used, as that might give some clues as to why it isn't working.

  • Hi Alan,

    thanks for the answer, I read that the PHPMailer is deprecated in two post but if it works I can use it, it's not a problem.

    I try to explain all my attempts with code blocks and result in the log files.

    I test three approches but no one is correct:

    1-code with MailerFactory (no documentation, only this post Send Emails programmatically from SugarCRM 7.5 | Miraz Al-Mamun )
       
    $phpMailer
    = MailerFactory::getSystemDefaultMailer();
    $emailIdentity = new EmailIdentity('test@hotmail.it', 'Mapo');
    //add the recipient
    $phpMailer->addRecipientsTo(array($emailIdentity));
    $phpMailer->setHtmlBody("Contratto ". $contratto['name']);
    try {
        $phpMailer->send();
        $GLOBALS['log']->fatal("The email was send successfully");
    } catch (Exception $e) {
        $GLOBALS['log']->fatal($e->getMessage());
    } finally {
        //TODO: the stuffs you would do anyway
    }


    RESULT:
    09/08/16 08:04:11 [5012][1][FATAL] Job a8ee26c7-edfe-a7f5-a2b6-57d11a16c561 (Rinnovare) failed in CRON run



    2-code with SugarPHPMailer (from this post https://community.sugarcrm.com/thread/28095?q=how%20to%20send%20email )
    require_once('include/SugarPHPMailer.php');
    $emailObj = new Email();
    $defaults = $emailObj->getSystemDefaultEmail();
    $GLOBALS['log']->fatal("mail: ".$defaults['email']);
    $GLOBALS['log']->fatal("name: ".$defaults['name']);
    $mail = new SugarPHPMailer();
    $mail->setMailerForSystem();
    $mail->From = $defaults['email'];
    $mail->FromName = $defaults['name'];
    $mail->Subject = 'Subject';
    $mail->Body = 'Body';
    $mail->prepForOutbound();
    $mail->AddAddress('example@hotmail.it');
    @$mail->Send();
      
    RESULT:
    09/08/16 08:11:19 [15024][1][FATAL] mail: default@gmail.com
    09/08/16 08:11:19 [15024][1][FATAL] name: SugarCRM
    09/08/16 08:11:19 [15024][1][FATAL] SMTP -> ERROR: Failed to connect to server. Code: 43413376 Reply: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
    09/08/16 08:11:19 [15024][1][FATAL] SugarPHPMailer encountered an error: SMTP Connect() failed.



    3- code with BeanFactory Emails (from this post https://community.sugarcrm.com/thread/28095?q=how%20to%20send%20email)
    $email = BeanFactory::newBean('Emails');
    $email->from_addr = "default@gmail.com";
    $email->from_name = "Sugarcrm";

    $email->reply_to_name = "default@gmail.com";
    $email->reply_to_email = "Sugarcrm";
    $email->name = "Subject";
    $email->description_html = "Body";
    $email->to_addrs_arr = "example@hotmail.it";
    $email->parent_type = 'Contacts';
    $email->parent_id = "9ed1bad0-615a-ff28-8cf1-57cec5fb4c8b";
    $email->save();
    $email->send();


    RESULT: 
    09/08/16 08:18:20 [12368][1][FATAL] SMTP -> ERROR: Failed to connect to server. Code: 358891786 Reply: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
    09/08/16 08:18:20 [12368][1][FATAL] MailerException - @(SmtpMailer.php:166 [6]) - Failed to connect to outbound SMTP Mail Server
Reply
  • Hi Alan,

    thanks for the answer, I read that the PHPMailer is deprecated in two post but if it works I can use it, it's not a problem.

    I try to explain all my attempts with code blocks and result in the log files.

    I test three approches but no one is correct:

    1-code with MailerFactory (no documentation, only this post Send Emails programmatically from SugarCRM 7.5 | Miraz Al-Mamun )
       
    $phpMailer
    = MailerFactory::getSystemDefaultMailer();
    $emailIdentity = new EmailIdentity('test@hotmail.it', 'Mapo');
    //add the recipient
    $phpMailer->addRecipientsTo(array($emailIdentity));
    $phpMailer->setHtmlBody("Contratto ". $contratto['name']);
    try {
        $phpMailer->send();
        $GLOBALS['log']->fatal("The email was send successfully");
    } catch (Exception $e) {
        $GLOBALS['log']->fatal($e->getMessage());
    } finally {
        //TODO: the stuffs you would do anyway
    }


    RESULT:
    09/08/16 08:04:11 [5012][1][FATAL] Job a8ee26c7-edfe-a7f5-a2b6-57d11a16c561 (Rinnovare) failed in CRON run



    2-code with SugarPHPMailer (from this post https://community.sugarcrm.com/thread/28095?q=how%20to%20send%20email )
    require_once('include/SugarPHPMailer.php');
    $emailObj = new Email();
    $defaults = $emailObj->getSystemDefaultEmail();
    $GLOBALS['log']->fatal("mail: ".$defaults['email']);
    $GLOBALS['log']->fatal("name: ".$defaults['name']);
    $mail = new SugarPHPMailer();
    $mail->setMailerForSystem();
    $mail->From = $defaults['email'];
    $mail->FromName = $defaults['name'];
    $mail->Subject = 'Subject';
    $mail->Body = 'Body';
    $mail->prepForOutbound();
    $mail->AddAddress('example@hotmail.it');
    @$mail->Send();
      
    RESULT:
    09/08/16 08:11:19 [15024][1][FATAL] mail: default@gmail.com
    09/08/16 08:11:19 [15024][1][FATAL] name: SugarCRM
    09/08/16 08:11:19 [15024][1][FATAL] SMTP -> ERROR: Failed to connect to server. Code: 43413376 Reply: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
    09/08/16 08:11:19 [15024][1][FATAL] SugarPHPMailer encountered an error: SMTP Connect() failed.



    3- code with BeanFactory Emails (from this post https://community.sugarcrm.com/thread/28095?q=how%20to%20send%20email)
    $email = BeanFactory::newBean('Emails');
    $email->from_addr = "default@gmail.com";
    $email->from_name = "Sugarcrm";

    $email->reply_to_name = "default@gmail.com";
    $email->reply_to_email = "Sugarcrm";
    $email->name = "Subject";
    $email->description_html = "Body";
    $email->to_addrs_arr = "example@hotmail.it";
    $email->parent_type = 'Contacts';
    $email->parent_id = "9ed1bad0-615a-ff28-8cf1-57cec5fb4c8b";
    $email->save();
    $email->send();


    RESULT: 
    09/08/16 08:18:20 [12368][1][FATAL] SMTP -> ERROR: Failed to connect to server. Code: 358891786 Reply: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?
    09/08/16 08:18:20 [12368][1][FATAL] MailerException - @(SmtpMailer.php:166 [6]) - Failed to connect to outbound SMTP Mail Server
Children