Custom module can't send emails on logic hook...

Hi there!

I need help, some days ago I changes the suite CRM files to another server and all is working fine, but I have one custom module and I have in that module some logic hooks working fine but one of them is not working fine, and that logic hook is in charge to send email after saving the record, I try many different ways but I can’t find the issue, my actual code is this:


                $GLOBALS['log']->fatal("Email = " . $tu);

                if ($bean->created_by == $tu)
                    continue;

                $user = new User();
                $user->retrieve($tu);
                $user_email = $user->emailAddress->getPrimaryAddress($user);
                if (trim($user_email) != '') {

                    $GLOBALS['log']->fatal("Email = " . $user_email);
                    require_once("modules/Emails/Email.php");

                    $mail = new SugarPHPMailer();
                    $emailObj = new Email();
                    $defaults = $emailObj->getSystemDefaultEmail();

                    $mail->From = $defaults['email'];
                    $mail->FromName = $defaults['name'];

                    $mail->ClearAllRecipients();
                    $mail->ClearReplyTos();

                    $mail->AddAddress($user_email, $user->first_name . " " . $user->last_name);
                    $mail->Subject = from_html($subject);

                    $mail->Body_html = from_html($body_html);
                    $mail->Body = wordwrap($body_html, 900);
                    $mail->IsHTML(true); //Omit or comment out this line if plain text
                    $mail->prepForOutbound();
                    $mail->setMailerForSystem();

                    //Send the message, log if error occurs
                    if (!$mail->Send())
                        ; {
                        $GLOBALS['log']->fatal('*** ERROR: Email Send Failed');
                    }
                }

the previous code is working on my original server but not in the new… and…

this code:


require_once 'include/phpmailer/class.phpmailer.php';
require_once 'include/phpmailer/class.smtp.php';

$mail = new PHPMailer;

$mail->SMTPDebug=14;
$mail->Debugoutput='html';
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';//   // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'sample@company.com;                  // SMTP username
$mail->Password = 'XXXXXXXX';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
$mail->From = 'sample@company.com'; 
$mail->FromName = 'Edward';
$mail->addAddress('sample@gmail.com', 'Eduardo');     // Add a recipient
//$mail->addReplyTo('source.name@gmail.com', 'Gmail Person Name');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

is working in different controller file and I try with the previous code on my custom module but not works…

somebody knows what could be happening?.. please help!

I saw the errors of the Email… and show me things like “Error authentication:” or “SMTP server can’t connect”, but all SMTP configurations are OK :S

in resume, I tried to send TEST Email from Admin->EMAIL SETTINGS-> and I hit “SEND TEST EMAIL” button but appear one alert saying: “Error: SMTP Error: Could not connect to SMTP host.”. (but in other modules of the system the Emails are sent fine.)

HELP!!!

Un-comment this line

https://github.com/salesagility/SuiteCRM/blob/master/include/SugarPHPMailer.php#L459

and check in the logs (debug level) the values that are being used immediately before sending.

You can also un-comment these lines below:

https://github.com/salesagility/SuiteCRM/blob/master/include/SugarPHPMailer.php#L500-L506

So you get a full SMTP log. Normally the authentication problems (assuming your credentials are correct) is related to mismatches between “From name” and your actual email address.

this looks like an authentication issue rather than a code issue, can you check you server´s logs? also if you’re using gmail there’s a feature you need to enable at gmail to permit relay from external application like these.

best regards