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)

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

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

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

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


                    $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

                    //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->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';//   // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = ';                  // SMTP username
$mail->Password = 'XXXXXXXX';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
$mail->From = ''; 
$mail->FromName = 'Edward';
$mail->addAddress('', 'Eduardo');     // Add a recipient
//$mail->addReplyTo('', 'Gmail Person Name');

$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.)


Un-comment this line

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

You can also un-comment these lines below:

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