Frank
Frank

Reputation: 207

PHPMailer cli works, cgi does not, why?

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      = '[email protected]';
$email->FromName  = 'Frank';
$email->Subject   = 'Test Email';
$email->Body      = $bodytext;
$email->AddAddress( '[email protected]' );

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      = '[email protected]'; # replaced by google with Username
$email->FromName  = 'Appears in From ahead of your gmail address';
$email->Sender    = '[email protected]'; # not visible to recipient
$email->Subject   = 'Appears in the subject field';
$email->Body      = $bodytext;
$email->AddAddress( '[email protected]' );
$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

Upvotes: -1

Views: 649

Answers (1)

user9677845
user9677845

Reputation:

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

Upvotes: 0

Related Questions