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.
Related
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...
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.
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.
I'm setting up a PHP script that uses PHPMail to send email. The "from" address is already verified on Amazon's console, and I have created the IAM user and SMTP credentials. When creating those, Amazon tells you to use ports 25, 465 or 587. Here's a php example from Amazon's documentation, doesn't use PHPMail but the idea should be the same.
This is my script:
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->Timeout = 20;
$mail->CharSet = 'UTF-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-east-1.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my_username'; // SMTP username
$mail->Password = 'my_password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('my#email.com', 'My email name');
$mail->addAddress('test#test.com', 'Test'); // Add a recipient
$mail->addReplyTo('my#email.com', 'My email name');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Amazon SES SMTP test with PHPMailer';
$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';
}
That script like that doesn't work, I get a connection timeout error. However, if I change the port to 443, then it works fine. Why is that? That's not a port listed by Amazon so I'm worried that even though it works now it might give some other problems in the future. Am I missing something here? Port 465 doesn't work either, by the way.
EDIT
Just for clarification, I realize this works using port 443 like I mention, however why isn't this working with the ports they suggest? That's what I'm trying to understand. Is there anything missing in this script? I've also teste without pre-fixing the host with "ssl://" (which is how they show it in their example) and using the suggested ports, to no avail.
TCP port 443 is the standard TCP port that is used for websites which use SSL. your address is
ssl://email-smtp.us-east-1.amazonaws.com
so to me this is working as it should.
The problem was that there was a setting in our CSF Firewall called SMTP_BLOCK that was on. We turned that off and now port 587 works fine (I've had also to remove the ssl:// from the host address).
Maybe this helps someone in the future with the same problem.
I am trying to send an email using gmail SMTP settings.
I tried in my localhost , it worked .
I downloaded the phpMailer , in that folder I wrote a PHP file with following contents.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail#gmail.com'; // SMTP username
$mail->Password = 'mygmailpassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'myfromadress#gmail.com';
$mail->FromName = 'Mailer';
$mail->addAddress('mytoadress.com', 'My name'); // Add a recipient
$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';
}
It worked in local system, But not working in the live server.
I am using Godaddy's Linux shared hosting service.
I live server its throwing the below error.
Message could not be sent.Mailer Error: SMTP connect() failed.
I cannot comment on your question because I don't have enough credentials... So I have to write an answer even though it's not technically an answer... I had similar problems a while ago. The bug was in my code. The short story is, turn on debug mode and you will have the exact reason why gmail is refusing to connect.
I'm with GoDaddy. You can't use Gmail's SMTP server. You need to use our relay server. Take a look at https://support.godaddy.com/help/article/953/what-is-the-name-of-my-hosting-accounts-relay-server for details. Feel free to follow up with me on Twitter #GoDaddyHelp if you have other questions. ^Nate