How do I get PHPMailer to send a test email? - php

I have a local Apache2 server running on Debian/Bullseye. I've been pulling my hair out trying to get PHPMailer to do anything. There seem to be 2 different methods for installing PHPMailer - 1 is to use composer, which is the first one I tried. It creates a vendor folder in the site's root, which includes an autoload.php file, among other things. That file contains:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitd359baac21f520c04e608f4eed750560::getLoader();
Which doesn't look like it's complete (no closing tag). Anyway, I can't get the "test.php" samples to work.
The other method is to download the .zip file from the gethub site and extract it into the site's root. This, after renaming, gives me a PHPMailer folder. Using the "mailer.php" samples doesn't do anything either.
In both cases I've modified the smtp information to use the domain's actual account information (sending e-mail, login password, smtp server name, host's smtp security and port settings) but I'm not even getting it rejecting the mail. Nothing is happening. I'm left with a blank web page.
I do have php running, as a previous php script I'd used still works (from my test site - the live site now insists on smtp and won't let me install PEAR modules).
Here's the mailer.php script I'm working with - with some details hidden:
<?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 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.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 = 'mail.<domain>.ca'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'mail#<domain>.ca'; //SMTP username
$mail->Password = '<secret>'; //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('mail#<domain>.ca', ‘from me’);
$mail->addAddress('gary#<domain>.ca', ‘to me’); //Add a recipient
// $mail->addAddress('Recipient#emailaddress.com'); //Name is optional
// $mail->addReplyTo('yourname#domain.com', ‘Your Name’);
// $mail->addCC('cc#example.com');
// $mail->addBCC('bcc#example.com');
//Attachments Optional
// $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 in bold!';
$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 TLS line are from the hosting company so I assume they are correct.
I've commented out options that I don't need for testing but it still doesn't work. Can anyone figure out what I'm doing wrong?
Thanks.

OK, got it. The host company provided the test code and their sample used smtp.domain.com. In actuality, their smtp server is mail, not smtp. I guess that when faced with a non-existent server, the code hangs...

Related

Troubleshooting PHPMailer with Bluehost

Trying to set up PHPMailer for a site I have hosted on Bluehost and after a full day of researching and troubleshooting I just can not get it working.
I'm a beginner so apologies in advance for the newbie question, but I've been reading everything I can find (including this and this, as well as the PHPMailer docs) to solve this but can't seem to get mine set up correctly. Any guidance, thoughts on what I'm doing wrong or ways to debug this are much appreciated.
This is what I've found regarding Bluehost SMTP.
Secure SSL/TLS Settings (Recommended)
Username Your email address: john#example.com
Password The password for that email account.
Incoming Server mail.example.com*
Incoming Port 993 (IMAP) or 995 (POP3)
Outgoing Server mail.example.com*
Outgoing Port 465 (SMTP)
Authentication Password
*Replace example.com with your domain name.
Below is what I'm using in my file (personal info removed).
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.MYDOMAIN.com';
$mail->SMTPAuth = true;
$mail->Username = 'MYEMAIL#MYDOMAIN.com';
$mail->Password = 'MYEMAILPASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 465;
//Recipients
$mail->setFrom('MYEMAIL#MYDOMAIN.com');
$mail->addAddress('MYEMAIL#MYDOMAIN.com');
//Content
$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;
}
I've installed PHPMailer using Composer so the autoload.php file (and the phpmailer folder) are in a 'vendor' folder located in the same directory as the file that contains the code shown above.
After uploading to my Bluehost server, when I try to display the webpage that has this code in the browser, I get this HTTP ERROR 500 screenshot and of course no email sent.
Something that's covered in many of the examples and the docs is what combinations of encryption and port settings will work.
You have Port = 465 and SMTPSecure = 'tls'; that won't work. Either change Port to 587, or SMTPSecure to 'ssl' (but not both!). As it stands, you're trying to open a connection to a port that expects implicit TLS while using a protocol that expects to need to make it explicit with STARTTLS, and that's not going to work.

PHP - Sending mail from XAMPP using PHPMailer [duplicate]

I have been trying to send a mail using php mailer on xampp and i do get this error saying
Message could not be sent. Mailer Error: The following From address failed: xxxx2#gmail.com : Called Mail() without being connected
please, i need help on how to fix this.
Here is my code;
<?php
require( 'class.phpmailer.php' );
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 25;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxxx";
//Sending the actual email
$mail->setFrom('xxxx2#gmail.com', 'Aaron');
$mail->addAddress('xxxx2#gmail.com', 'Aaron'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Calculation form results from ';
$mail->Body = 'testing...';
if(!$mail->send()) {
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
Literally copied from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//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
$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 = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
I tried with Composer and github, but couldn't get the latest version of phpmailer to work with my test php email through xampp. When I ran the program on localhost, it kept crashing out, as the email code was looking for the php mailer class.
So I went back to google, ignored composer, and downloaded a plain ordinary zip of php mailer 5.2.0, and extracted this zip directly into my 'websiteX' testing folder which is located in htdocs in xampp.
In my 'websiteX' folder I have my testmail.php file along with my phpmailer unzipped folder and it contains the class.phpmailer which actually works in my case.
I have spent a week faffing around, but now I have xampp php emails going to my test gmail account perfectly. I use chrome and notepad++ as well.
Funnily enough I had php emails with the php mail() command working too, although Gmail hard bounced 'em, which isnt great. The first thing I did last week was to get Mercury mail (included in xampp) working. I followed this link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows and managed to get xampp communicating with gmail which was great!
Good luck with your coding.

new PHPMailer is not running?

Below is the phpmailer code which I am using :
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = as given in the configure mail client window;
$mail->SMTPAuth = true;
$mail->Username =as given in the configure mail client window;
$mail->Password = as given in the configure mail client window;
$mail->SMTPSecure = 'tls' ;
$mail->Port = 465;
$mail->setFrom('my email', 'my name');
$mail->addAddress('email', 'name');
$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;
}
The page keeps loading and the e-mail is not sent,What is happening ? and how should I configure this . I am using godaddy cpanel.
You're using SMTP to localhost (which is the recommended and usually the fastest sending mechanism, as far as your script is concerned), but you've enabled encryption and authentication, so your local mail server will need to present a valid certificate for localhost, which is not going to happen. It's usually unnecessary to use encryption or authentication when sending to localhost, because you can whitelist localhost as the source, and this will make it even faster.
If you set SMTPDebug = 2, you can look at the timestamps in the SMTP conversation and see which part is taking a long time.
Keepalive won't help unless you're sending lots of messages in quick succession.
It may also help to look in your local mail server's logs and see if there's anything interesting in there.
You're also using a very old version of PHPMailer; get the latest, and base your code on the examples provided.
You should have no trouble submitting a few hundred messages per second.
If your problem is that submission is fast, but ultimate delivery is slow, you need to look at your local mail server logs for why that is. You may be getting delivery deferrals.

how to send mail using phpmailer on xampp

I have been trying to send a mail using php mailer on xampp and i do get this error saying
Message could not be sent. Mailer Error: The following From address failed: xxxx2#gmail.com : Called Mail() without being connected
please, i need help on how to fix this.
Here is my code;
<?php
require( 'class.phpmailer.php' );
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 25;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxxx";
//Sending the actual email
$mail->setFrom('xxxx2#gmail.com', 'Aaron');
$mail->addAddress('xxxx2#gmail.com', 'Aaron'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Calculation form results from ';
$mail->Body = 'testing...';
if(!$mail->send()) {
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
Literally copied from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//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
$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 = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
I tried with Composer and github, but couldn't get the latest version of phpmailer to work with my test php email through xampp. When I ran the program on localhost, it kept crashing out, as the email code was looking for the php mailer class.
So I went back to google, ignored composer, and downloaded a plain ordinary zip of php mailer 5.2.0, and extracted this zip directly into my 'websiteX' testing folder which is located in htdocs in xampp.
In my 'websiteX' folder I have my testmail.php file along with my phpmailer unzipped folder and it contains the class.phpmailer which actually works in my case.
I have spent a week faffing around, but now I have xampp php emails going to my test gmail account perfectly. I use chrome and notepad++ as well.
Funnily enough I had php emails with the php mail() command working too, although Gmail hard bounced 'em, which isnt great. The first thing I did last week was to get Mercury mail (included in xampp) working. I followed this link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows and managed to get xampp communicating with gmail which was great!
Good luck with your coding.

how send mail without changing hosting files (or without entering credential in hosting files static)

I searched a lot how to send email but what i found was for specific criteria.2 are given follow;
like how to mail form localhost (by provoding detail in sendmail.ini and other custom code)
how to sen mail form godaddy hosting (use PHP Form Mailer)
If i follow this them it becomes specific by changing hosting files.
while there are many scripts(developed sites for sale) which allow users to install those scripts on localhost or any hosting plan and enter smtp credential and those script able to send mail(contact-us form) without changing hosting files.
but what i know is to give credential in 'sendmail.ini' file (id, password, smtp server) to send mail or use PHP Form Mailer for godaddy hosting.
but actually i'm developing website and i will put it on 'codycanon' or other platform to sale this, so in this case user can host it on any hosting he likes. so can you guide me how i can send mail without changing hosting files?
use PHP MAILER script https://github.com/PHPMailer/PHPMailer
include it in your project and use it like this
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
All your user has to do is use the SMTP details provided by his hosting provider and enter it in the relevant variables, you can go a notch higher and make a interactive html page where your user enters details and your script automates the process of editing the config.
SwiftMailer is also a potential alternative.

Categories