1

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   = "XXXXXXXX@gmail.com";
$mail->Password   = "XXXXXXXX";           

$mail->Subject    = "TEST";
$mail->MsgHTML($body);
$address = "YYYYYYYY@gmail.com";
$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

Tom
  • 1,027
  • 2
  • 16
  • 35
  • 1
    are you just trying to run it in the background and nothing else? – Kevin Kopf Feb 09 '16 at 19:43
  • Yes I want to run it in background and I know that I can do it this way: shell_exec("nohup php send.php"). But now I'm getting this error. The problem started to began when I upgraded php5 to php7. Maybe this is important information – Tom Feb 09 '16 at 19:48
  • 1
    what is your system and how did you install php7? I suspect via a simple ./configure? Run `phpinfo()` and see if OpenSSL is enabled. If not, reinstall PHP 7 with `--with-openssl` flag – Kevin Kopf Feb 09 '16 at 19:52
  • I checked `phpinfo()` and there is `--with-openssl` flag in Configure Command section. Also OpenSSL support is set to `enabled`. I compiled php7 myself with ./configure. It is possible that I did something wrong. The system is Raspbian on Raspberry – Tom Feb 09 '16 at 19:57
  • 1
    Okay, try `$mail->SMTPSecure = "ssl";` instead of `tls`. If this doesn't work, it's could be an extensive server issue that I'm afraid I can't solve telepathically – Kevin Kopf Feb 09 '16 at 20:02
  • I tried with `ssl` and port `465`. Also by calling direct `send.php` - the mail is sent. By calling `script.php` now I'm getting different error: `SMTP ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?` I think I will just try to recompile php and install again. Thank you for your help :) – Tom Feb 09 '16 at 20:11
  • 1
    Use `tls` on 587. It looks like you've based your code on an obsolete example - use the ones provided with PHPMailer. If you load the autoloader, you don't need to load the other classes. – Synchro Feb 09 '16 at 23:40
  • @Synchro You have right. I actually don't need any other classes, but I have checked and this does not change anything ;) – Tom Feb 10 '16 at 18:04

0 Answers0