-1

I have two machines with up to date copies of Ubuntu 16.04, php 7 and PHPMailer installed. EMails are sent via gmail using ssmtp and it's associated sendmail stub. If I run the same program (effectively the code out of the tutorial in the Wiki) from the command line and the cgi (Apache2) the command line works and the cgi does not and give the error message "Mailer error: Could not instantiate mail function."

Please don't say read the troubleshooting guide I have and I'm sorry but a small criticism it's a bit cryptic, I don't understand. (Something we all do trying to keep it short and simple.)

However, if I check the php.ini file for both they are the same in the areas that affect email.

Is there a required difference that I have missed? What is wrong?

<?php

require_once('phpmailer.php');
require_once('exception.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$bodytext = "This is a test EMail sent from the sever.    

Frank\n";

$email = new PHPMailer();
$email->From      = 'reception@xxx.xxx.uk';
$email->FromName  = 'Frank';
$email->Subject   = 'Test Email';
$email->Body      = $bodytext;
$email->AddAddress( 'frank@xxxx.xxx.uk' );

return $email->Send();

echo "Done";

?>

I know it's junk for the cgi but the point is it should run in both.

The mail.err log shows

Apr 30 10:14:02 Desktop-Frank sSMTP[5579]: Unable to connect to "smtp.gmail.com" port 597.
Apr 30 10:14:02 Desktop-Frank sSMTP[5579]: Cannot open smtp.gmail.com:597

Which correlates with the front end error message.

My guess is a piece of information missing or different between the two methods. The returned error message from ssmtp is being misunderstood.

I have even tried updating the php.ini to give the fully resolved path to the sendmail binary stub.

Can anybody help me resolve this?

Thanks Frank

UPDATE: Working code in both environments

I still don't understand why the original code did not work in the cgi environment but good news the following works in both cgi and cli!

Remember to put phpmailer.php, exception.php and smtp.php into /usr/share/php/ and watch the file names.

<?php

require_once('phpmailer.php');
require_once('exception.php');
require_once('smtp.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

$bodytext = "This is a test EMail sent from the sever.";

$email = new PHPMailer();
$email->From      = 'xxx@xxx.xx.xx'; # replaced by google with Username
$email->FromName  = 'Appears in From ahead of your gmail address';
$email->Sender    = 'xxx@xxx.xx.xx'; # not visible to recipient
$email->Subject   = 'Appears in the subject field';
$email->Body      = $bodytext;
$email->AddAddress( 'xxx@xxxx.xx.xx' );
$email->AddAttachment( '/fullpath/file.name' );
$email->isSMTP();
$email->SMTPAuth = true;
$email->SMTPDebug = 4; # lots of debug 0 for production
$email->SMTPSecure = 'tls';
$email->Port = 587;
$email->Username = 'gmail email address';
$email->Password = 'gmail password';
$email->Host = 'smtp.gmail.com';

echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Value</td></tr>\n";
foreach($email as $key=>$item) {
    echo "<tr><td>".$key."</td><td>".$item."</td></tr>\n";
}
echo "</table>";

return $email->Send();

echo "Done";

?>

Thanks to everybody particularly Synchro for his patience.

Frank

Frank
  • 207
  • 2
  • 6
  • I'd advise skipping the local mail server and use SMTP directly, if you can. – Synchro Apr 30 '18 at 13:31
  • I'd love to but don't have the understanding to set up the direct connection. – Frank Apr 30 '18 at 18:53
  • That's why [there are examples](https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps) provided with PHPMailer. – Synchro Apr 30 '18 at 19:20
  • Synchro - Thanks/Sorry I had not tried the example you pointed out. However, I have now and .. PHP Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\SMTP' not found in /usr/share/php/phpmailer.php:1703 Stack trace: #0 PHPMailer->getSMTPInstance() #1 PHPMailer->smtpConnect(Array) #2 PHPMailer->smtpSend('Date: Tue, 1 Ma...', 'This is a multi...') #3 PHPMailer->postSend() #4 /home////gmail.php(72): PHPMailer->send() #5 {main} thrown in /usr/share/php/phpmailer.php on line 1703 My only changes are to use my email addresses and password And inhibit the image attachement. – Frank May 01 '18 at 15:53
  • You are not using composer or you did not load the SMTP class yourself. See loading instructions in the readme. – Synchro May 01 '18 at 19:17
  • Thanks Synchro sometimes the simplest errors are the hardest to spot. I have posted (above) the final code that works in both environments. – Frank May 03 '18 at 08:33

1 Answers1

0

Enable SMTP secure option and also change port to 465.

$mail->SMTPSecure = 'ssl'; 
$mail->Port       =  465;                    // set the SMTP port

Set SMTP mail password

$mail->Password   = "mailpassword";  // SMTP password
  • Sorry tried adding these to the above code but it makes not difference. I am still hunting elsewhere but no god so far. – Frank Apr 30 '18 at 18:56