Can't send mail through exec in PHP

555 Views Asked by At

I'm trying to send an email and I have 2 scripts:

send.php

<?php

require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';

$mail             = new PHPMailer();
$body             = " ";
$mail->IsSMTP(); 
$mail->SMTPDebug  = 4; 
$mail->SMTPAuth   = true;                
$mail->SMTPSecure = "tls";                
$mail->Host       = "smtp.gmail.com";    
$mail->Port       = 587;                
$mail->Username   = "[email protected]";
$mail->Password   = "XXXXXXXX";           

$mail->Subject    = "TEST";
$mail->MsgHTML($body);
$address = "[email protected]";
$mail->AddAddress($address, "");

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>

script.php

<?php
echo shell_exec("php send.php");
?>

By calling directly send.php in browser, the email is send. Problem is when I'm calling script.php, then the email is not send and Im getting an error. Here are last lines from output:

2016-02-09 19:19:55 Connection: opening to smtp.gmail.com:587, t=300, opt=array (
                                      )
2016-02-09 19:19:55 Connection: opened
2016-02-09 19:19:55 SMTP -> get_lines(): $data was ""
2016-02-09 19:19:55 SMTP -> get_lines(): $str is "220 smtp.gmail.com ESMTP cc7sm606507lbd.11 - gsmtp
                                      "
2016-02-09 19:19:55 SMTP -> get_lines(): $data is "220 smtp.gmail.com ESMTP cc7sm606507lbd.11 - gsmtp
                                      "
2016-02-09 19:19:55 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP cc7sm606507lbd.11 - gsmtp
2016-02-09 19:19:55 CLIENT -> SERVER: EHLO raspberrypi
....
Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in /var/www/html/class.smtp.php on line 325
2016-02-09 19:19:55 CLIENT -> SERVER: QUIT
2016-02-09 19:19:56 SMTP -> get_lines(): $data was ""
2016-02-09 19:19:56 SMTP -> get_lines(): $str is "�F"
2016-02-09 19:19:56 SMTP -> get_lines(): $data is "�F"
2016-02-09 19:19:56 SERVER -> CLIENT: �F
2016-02-09 19:19:56 SMTP ERROR: QUIT command failed: �F
2016-02-09 19:19:56 Connection: closed
2016-02-09 19:19:56 SMTP connect() failed.
Mailer Error: SMTP connect() failed.

What can be causing this issue? Im using PHP7

0

There are 0 best solutions below