I've done some searching before this post and I still can't seem to get this to work. I'm trying to setup a cron job with PHPMailer to send out an email every so often. The script below does work if I run it manually but does not work in the cron job scheduler.
For this example - I set it to run every minute. I'm thinking it has to do something with the "vendor/autoload.php" and it's path not loading correctly? I didn't add my SMTP credentials with api key for security reasons as well as recipients for this post.
Here is my cron job setup in Cpanel.
Here is my PHPMailer code:
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
// Server settings
// $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = ''; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
// Recipients
$mail->setFrom('email#email.com', '');
$mail->addAddress('email#email.com', ''); // Add a recipient
$mail->addReplyTo('email#email.com', '');
// $mail->addCC('cc#example.com');
// $mail->addBCC('');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'PHPMailer email';
// $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->msgHTML(file_get_contents('email.html'), __DIR__); // Use this if not using the above code
// ********* PHP-MAILER ********* //
$mail->send();
echo 'Email sent!';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
If anyone could help me, I would really appreciate it!
Can you share you error-message with us? I assume this will help a lot to find the issue.
I've shared my insights how to enable logging in a different post on stack overflow (see link below). This will explain how you can display errors in your cron execution:
https://stackoverflow.com/a/60250715/12880865
Please let me know if this helps you. If you'll get a proper error message, please share it with us so we can dig further into your question.
Fixed!
I had to use (dirname(DIR) which is the directory of the file.
I Changed:
require 'vendor/autoload.php';
to:
require (dirname(__DIR__).'/mailer/vendor/autoload.php');
Related
I keep getting error when trying to send email using PHP mailer (localhost). or does php mailer not work on localhost?
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'default#gmail.com'; //SMTP username
$mail->Password = '00000120'; //SMTP password
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('NatsuDragneelxd42069#gmail.com', 'Mailer');
$mail->addAddress('Received#gmail.com', 'Joe User'); //Add a recipient
//$mail->addAddress('ellen#example.com'); //Name is optional
$mail->addReplyTo('Noreply#gmail.com', 'Info');
// $mail->addCC('cc#example.com');
// $mail->addBCC('bcc#example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$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}";
}
This is the error that I get:
SERVER -> CLIENT:
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
I don't know why you commented out this line, but it will make the connection fail because it will attempt to make an unencrypted connection to a port expecting encryption:
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
Uncomment that and you might have more luck. You also might want to try setting SMTPDebug = SMTP::DEBUG_CONNECTION as it will give you more info about the TLS phase of the connection.
This may not solve your whole problem because Gmail no longer (as of May 2022) allows authentication using regular ID and password. You need to either switch to using XOAUTH2 (which PHPMailer supports), or create an App Password in your gmail console.
Also note that gmail won't let you use arbitrary from addresses, only your Username address and predefined aliases.
All this is covered in the PHPMailer troubleshooting guide.
all I need to do was fix the less secured but since it's been removed by google itself I search for an alternative less secure apps alternative
I am trying to use PHPMailer to send email, and I'm writing the PHP in cPanel through Bluehost. Below is my code so far. The file is in the PHPMailermaster folder, along with the src folder.
The code fails upon reaching the "new PHPMailer" line (so when I run it, the page would echo "Reached checkpoint 1" but not "Reached checkpoint 2").
I have never used PHP before so it is very possible that I am doing the PHP wrong, and it seems like almost all tutorials about this are for older versions of PHPMailer, so all I really have to go off of is their GitHub page. My code is mostly copied from there, with a little modification for the use paths. I also commented out the autoloader because I don't have that and I'm not sure what it does.
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use src\PHPMailer;
use src\SMTP;
use src\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
// Load Composer's autoloader
// require 'vendor/autoload.php';
echo "Reached checkpoint 1";
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
echo "Reached checkpoint 2";
try {
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '<my username>'; // SMTP username
$mail->Password = '<my password>'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('<my email>#gmail.com');
$mail->addAddress('<test email>#gmail.com'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$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}";
}
The error thrown is the following (adminusername is just a placeholder for my cpanel username):
Fatal error: Uncaught Error: Class 'src\PHPMailer' not found in /home1/<adminusername>/public_html/PHPMailermaster/mail.php:21 Stack trace: #0 {main} thrown in /home1/<adminusername>/public_html/PHPMailermaster/mail.php on line 21
Check the 'namespace' clausule in "src/PHPMailer.php" file. It's probable that the PHPMailer class is not in the src namespace.
You should use now composer for installing it, after that, uncomment the require 'vendor/autoload.php'; line, and use something like:
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
More info in https://github.com/PHPMailer/PHPMailer
I believe I have solved the issue. To use PHPMailer without Composer, I created an include_directory, put the PHPMailermaster folder in there, and added it to php.ini using this method. I imported the necessary class files like so:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require '../include_directory/PHPMailermaster/src/Exception.php';
require '../include_directory/PHPMailermaster/src/PHPMailer.php';
require '../include_directory/PHPMailermaster/src/SMTP.php';
And it worked. I think the problem was how I was importing and organizing the files.
Fatal error: Class 'PHPMailer' not found in C:\wamp\www\sendemail.php on line 13
This is line 13:
$mail = new PHPMailer();
I have already researched and they say you need to have these:
require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
But I already have those and they reside in the same folder as sendemail.php but still the same error.
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "email#email.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('email#email.com', 'name');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('email#sample.com', 'name');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Here is my file layout:
I encountered the same issue, and managed to solve it by entering the following two lines at the very top of the PHP script used for sending the mail - not just in the files I required.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
?>
Let me know if it works for you as well.
Add all those extra require lines are not going to help; Just load the autoloader, like the original example code says. Try loading it from an absolute path:
require '/full/path/to/PHPMailerAutoload.php';
If that works, you need to check that your include_path setting in php.ini includes the directory you're loading from - for example that it contains . as one of the paths.
I'm pretty convinced this is an environment/config problem, not code, so try a completely minimal script, just this:
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
If you're doing new development, you should really be using composer anyway - it completely solves include problems and you'll never have to worry about where your libraries are again.
I'm trying to use phpmailer to work alongside a contact form I have made. I was originally using simple php coding for the form, but this would not work on the web company's server, so they asked me to use phpmailer instead. I have changed the coding a little bit to try and integrate it into the contact page that has the contact form.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->Username = "myemail#hotmail.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->addAddress('stacey_victoria#hotmail.com', 'Sender'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message from Website';
$mail->Body = $message; $name;
$mail->From = $email;
if($_POST){
$feedback = 'Thanks for your message';
}
?>
The contact page appears as normal, and once the submit button has been pressed, the echo-feedback message is shown. However, no email is sent through.
I edited the "Simple Example" found here: https://github.com/PHPMailer/PHPMailer and uploaded the php file to the server and this worked fine so I know the smtp information I have used is correct and working.
Is it even possible to put phpmailer coding above the html coding?
If so, what am I doing wrong?
You are not sending the email...
Use :
if($mail->Send())
I'm trying to get the basic example working for PHPMailer.
All I done was upload the whole PHPMailer_5.2.2 folder, configured the page below as per the code you see and I keep getting Mailer Error: Message body empty, but I can clearly see the contents.html file has html in it and isn't empty. This is the example file I'm using from the PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php
I tried using the settings I have in Outlook for Gmail that works, I know my username and password, the SMTP port is 587 and it's set to TLS, I tried replacing SSL with TLS in the code below, I still get same error.
I also tried the following code, which has been suggested:
changed this:
$mail->MsgHTML($body);
to this
$mail->body = $body;
I still got same error, is there something else I need to configure? It's my first time using PHPMailer, I can get the standard php mail working, but I want to try this because the page I'm going to be emailing has lots of html and I don't want to have to go through all the html and enter character escapes so someone recommending using this.
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = preg_replace('/[\]/','',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxx#gmail.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('xxx#gmail.com', 'First Last');
$mail->AddReplyTo("xxx","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "xxx.net";
$mail->AddAddress($address, "John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I had the same problem and I solved just changing:
This:
$mail->body = 'Hello, this is my message.';
Into this:
$mail->Body = 'Hello, this is my message.';
body into Body what a little mistake!
PHPMailer->MsgHTML() tries to do something clever with fixing non-absolute URLs, but for me too it just returns empty.
$mail->IsHTML(true);
$mail->Body=$body;
First I had to enable the ssl extension, (which I have obtained from another post on this site). To do that, open php.ini and enable php_openssl.dll by changing
;extension=php_openssl.dll
to
extension=php_openssl.dll
Now, enable tls and use port 587. Then comment out the preg_replace.
Finally, I was able to make it show up in my gmail "Sent" file, although I was expecting to see it in my "Inbox".
This site is awesome! Thanks.
When setting $mail->Body equal to variable the body is empty.
When I attach a string to the body everything works fine.
The solution is to strval() a variable containing Your HTML code.
$message;//Your HTML message code
$mail->Body = strval($message);