problem sending mail with PHPMailer on AWS AMI instance - php

I'm fairly new to UNIX, coming from a Windows background.
I've created an instance on Amazon EC2 and installed apache, PHP and MySQl.
i've successfully uploaded the files for a PHP website.
Everything works well, except i have a problem sending mails from a contact form.
i went through the AWS tutorial here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-php.html
i successfully installed composer and ran it in Putty and i see the vendor directory is created and the phpmailer files are downloaded.
site structure looks like this:
html
test_mail.php
--vendor
----bin
----composer
----phpmailer
----autoload.php
I've tried using the sample email script included in the tutorial which looks something like this :
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
// Instantiate a new PHPMailer
$mail = new PHPMailer;
// Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 2;
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$mail->setFrom('sender#example.com', 'Sender Name');
but i get the following error:
Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer' not found in /var/www/testSite/html/test_mail.php:10 Stack trace: #0 {main} thrown in /var/www/testSite/html/test_mail.php on line 10
line 10 is
$mail = new PHPMailer;
so i'm stumped as to what the problem is.
The required PHPMailer files seem to have been created correctly by composer, and the path to 'vendor\autoload.php' should be correct.
is it possible there's something in the server setup I've missed?
any suggestions gratefully received.
David

AWS Does not have autoload anymore, and PHPMailer should be initialized as follows:
<?php
require("/home/site/libs/PHPMailer-master/src/PHPMailer.php"); require("/home/site/libs/PHPMailer-master/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("xxxxxx#xxxxx.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxxx#xxxxx.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
} ?>

Thank you Lawrence for pointing me in the right direction...
The problem was, as you pointed out, with the version of phpmailer being used.
when i changed the composer.json for this project to
{
"require": {
"phpmailer/phpmailer":"~6.0"
}
}
and ran the composer update my scripts now run (mostly) successfully.
It looks like the example given in the AWS docs is incorrect as it says to use phpmailer 5.2, but the script it gives only works in version 6 onwards.
thank you!
David

Related

phpmailer not working, error log: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host

Recently I built a website, Everything is working smooth but contact form is not sending email. I am doing it using a combination of jquery, php and PHPMailer php library. Please help me out. Here is the code:
php in a separate file named contact.php in a folder PHP:
<?php
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '<myemail>';
$mail->Password = '<secretpassword>';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$to = "<myemail>";
$from = $_POST['email'];
$first_name = $_POST['name'];
$subject = "Contact form";
$message = "Message: " . $_POST['textans'];
$mail->SetFrom($from, $first_name);
$mail->addAddress('<myemail>', 'Diode');
$mail->Subject = $subject;
$mail->Body=$message;
$mail->send();
?>
According to the digging I have done the data is being passed to the PHP but is not being sent. I am using the website on AWS EC2. I checked telnet for connection, but telnet works so I am sure the host is reachable. I have also turned off the two-factor verification and turned on less secure apps.
I tried: 1) changing port to 465. 2) turning off autotls. 3) Checked that openssl is uncommented. 4) Checked inbound and outbound rules both in windows firewall and aws security
It give a post error (500 Internal server error) on the browser debugger. Here is the PHP log:
[<dateandtime> Asia/Kolkata] PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in <somelocation>\PHP\src\PHPMailer.php:2129
Stack trace:
#0 <somelocation>\PHP\src\PHPMailer.php(1953): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array)
#1 <somelocation>\PHP\src\PHPMailer.php(1635): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Th...', ' Name: N...')
#2 <somelocation>\PHP\src\PHPMailer.php(1468): PHPMailer\PHPMailer\PHPMailer->postSend()
#3 <somelocation>\PHP\contact.php(37): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
thrown in <somelocation>\PHP\src\PHPMailer.php on line 2129
Also it works on localhost on XAMPP!
Thanks for your help in advance.
First of all, I tried running your code which gave me SMTP Class not found error, so I added another use statement use PHPMailer\PHPMailer\SMTP; to avoid future errors.
Then, I got the same error and solved it by generating an App Password, which was required for 2-Step Verified accounts.
I didn't really change your code, but I ran it in localhost.
If nothing works, you can set the debug mode $mail->SMTPDebug = 3; in order to get more details, and also I recommend checking out the SSL/TLS protocols, which I saw it from this thread: PHPMailer: SMTP Error: Could not connect to SMTP host
I hope that I didn't waste your time...

Configuring PHPMailer on Siteground shared hosting plan

I am stuck trying to make PHPMailer to work with my website hosted on Siteground.
I installed it by composer via PuTTY, everything looking fine so far:
But when I try to test it, I can't get it to work properly.
Here is a sample of my testing code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = '**HOST**';
$mail->SMTPAuth = true;
$mail->Username = '**MAILACCOUNT**';
$mail->Password = '**PASSWORD';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('**MAILACCOUNT**', 'Mailer');
$mail->addAddress('**TESTMAILRECIPIENT', 'test');
$mail->isHTML(true);
$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';
$mail->send();
echo 'Message has been sent';
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Which is a modified version of the template PHPMailer provides on their github.
All of my credentials are good (host, mail account, password). I tried with different ports (25, 587) and as per Siteground the good port for SMTP is 465. But nothing work. The try catch block does not even catch any error, sometimes depending of what I try my webpage stays blank, or return a HTMLERROR 500.
I tried to reach out their technical support but they don't seem to have a clue about anything. They sent me the link to the PHPMailer github, telling me to follow the tutorial.
There is absolutely no documentation about using this PHP library with their services. I have been searching for 2 days now. It seems they have changed their interface, as many - if not all - of their tutorials are for an older version of their cPanel.
Maybe it could have something to do with SPF and DKIM protocols? DKIM is active on my account, SPF I have absolutely no clue what to do with this.
Here is my generated SPF record, if ever you need to know:
v=spf1 +a +mx +ip4:**MYIP** include:_spf.mailspamprotection.com ~all
I tried many online tutorials and adapted them to my needs with Siteground, with no success.
Is there anybody used to their system and could help me getting this to work?
I use a shared hosted plan, if it helps. They use Linux servers.
Thank you very much!
Just in case someone is looking for the same thing - for me what worked is the next:
prefixing the host with "ssl://"
in PHPMailer instance setting:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
Also the port I used was 465.
Very strange combination of settings, but at least it worked in my case.

PHPMailer autoload.php not able to open

I am currently hosting a website on GoDaddy and recently I added a contact form that will send me emails when people fill it out. It works perfectly when I use XAMPP, but once I uploaded the same code onto the GoDaddy servers, it gives me a HTTP ERROR 500.
I am using PHPMailer and Gmail's SMTP service. This is the current code that works with XAMPP:
<?php
require_once "vendor/autoload.php";
$name = $_POST["name"];
$email_from = $_POST["email"];
$telephone = $_POST["telephone"];
$message = "Name: ".$name."\r\n".
"Email: ".$email_from."\r\n".
"Telephone: ".$telephone."\r\n";
$email_to = "myemail#gmail.com";
if(isset($_POST['submit'])){
$mail = new PHPMailer\PHPMailer\PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "myemail#gmail.com";
$mail->Password = "password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->From = $email_from;
$mail->FromName = $name;
$mail->addAddress($email_to);
$mail->addReplyTo($email_from);
$mail->Subject = "Info";
$mail->Body = $message;
if ($_POST["submit"]){
if(!$mail->send()){
echo "Mailer error: " . $mail->ErrorInfo;
}
else{
echo "Message sent successfully";
}
}
}
Since it was giving me HTTP ERROR 500, I decided to look into the error logs given by GoDaddy. This is the error:
[13-Dec-2018 19:33:13 UTC] PHP Fatal error: require_once(): Failed opening required '/home/namesearch/public_html/vendor/composer/autoload_real.php' (include_path='.:/opt/alt/php56/usr/share/pear:/opt/alt/php56/usr/share/php') in /home/namesearch/public_html/vendor/autoload.php on line 5
Please help!!! I have looked up countless solutions and I just don't know how to fix it. I even tried putting my path as: /home/namesearch/public_html/vendor/composer/autoload_real.php and /home/namesearch/public_html/vendor/composer/autoload.php but nothing works.
I'm adding on the comment from #h2oooooo here. This is something that drove me insane when I first started learning composer. As he said above, make sure you've uploaded all of your dependencies, and then updates your autoload files. But, here's exactly how to do that:
1) Go to the root of your project in your local environment.
2) Then run composer update
3) I recommend just uploading the entire project again, unless you use filezilla folder compare to show which files are different, it'll be hard to know for sure.
If you're uploading your project from a git repo, then double check that your .gitignore file isn't ignoring your /vendor folder, as it often does by default.
I had a similar problem with configuring mustache so I hope this helps. My problem was with this line:
require_once "vendor/autoload.php";
It wasn't picking up the correct PHP include path.
Add the absolute path of the folder one level before vendor to your PHP include path.
Place this code right before the require.
$includePath = get_include_path() . ";ABSOLUTE PATH OF FOLDER ONE LEVEL BELOW VENDOR";
set_include_path($includePath);

Unable to send PHPMailer through gmail account from IIS on local machine

I'm running Windows 10 Pro with IIS hosting PHP 5.6.11. In the past, an older version of PHPMail worked perfectly for sending emails via gmail in this type of windows/IIS/PHP setup. At some point in the past it quit working. So now I'm using the very latest PHPMail library as of January 6, 2018. Here is my simple example taken mostly from this gmail example.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // TLS only
$mail->SMTPSecure = "tls"; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Here is the culprit:
Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [.....\PHPMailer\src\SMTP.php line 405]
How can I solve this?
UPDATE: I found the true source of the problem and updated the error message in this post, but I don't know how to solve it.
There are many case you have to check like
open SSL in enable using you php ini
you have to check your firewall that you are successfully able to access gmail server.
etc
you can find more solution and cases here
Change below value in your code.
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 2; //Get Error Details
Hope that's work.
I have tried everything that have been written on Stackoverflow but no luck. At last I omitted the line:
$mail->isSMTP();
Of course it is not a solution but in my case I had to solve the problem and it just did it. Now I'm using php's original mail() function. By the way I tried to use Gmail and Hotmail SMTP services. I don't want anyone to spend so much time on this like me.
Everything was solved when I installed a correct certificate in IIS.

PHPMailer with PHP 5.3.27

I am stuck with this issue. I tried to search it, but no help. I have a very small application running on my local WAMP. I have tested my application on a WAMP server,
and mailing service works perfectly. My WAMP has PHP 5.4, but when I deployed the same code on a hosting server(Network Solutions with PHP 5.3.27) it is not working. Below is
my code:
<?php
require 'PHPMailerAutoload.php';
/*
other code;
*/
//Mailing settings
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.server_name.net';
$mail->SMTPAuth = true;
$mail->Username = ' admin_user_name#server_name.net';
$mail->Password = 'password';
$mail->SMTPDebug = 1;
$mail->From = 'from_address#server_name.net';
$mail->FromName = 'from_name';
$mail->addAddress('receiver_add#abc.com', 'Receiver');
$mail->addCC('cc_address#abc.com','XYZ');
$mail->WordWrap = 50;
$mail->Subject = 'Subject_Was_Not_Long';
$body=" ABCD BODY.\n";
$mail->Body = $body;
if(!$mail->send()) {
header('Location: Same_File.php?registered=false');
} else {
header('Location: Same_File.php?registered=true');
}
?>
I am not sure what I have to do, because I am not able to access the PHP config as well. Please help!
Extra bit of info:
Configuration Hosting Server My PC
System Linux Windows
Server API CGI/Fast CGI Apache Handler 2.0
Virtual Directory Support Disabled Enabled
Thread Saftey Disabled Enabled
Thank You...
Had the same problem just now. The problem is fixed in the current GitHub version of PHPMailer. PHPMailer initially checks for version '5.0.0', but it contains the array syntax [] which is only active in version 5.4. If you replace all the [] in the PHPMailer classes with array(), it works.

Categories