Setup PHPMailer in a linux server GoDaddy - php

I extracted the PHPMailer zip in my server root and created the contact.php file to handle the contact form data but when i submit the form i get an error not one i used in my catch block get this error Oops! An error occured and your message could not be sent. i, need some help setting up the PHPMailerin my server. Here's my contact.php code
<?php
use PHPHMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
//
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'sxb1plzcpnl473190.prod.sxb1.secureserver.net';
$mail->SMTPAuth = true;
$mail->Username = 'info#***.co.za';
$mail->Password = '***';
$mail->SMTPSECURE = 'tls';
$mail->Port = 587;
// receipt
$mail->setFrom('My Mail');
$mail->addAddress($_POST['email']);
// content
$mail->isHtml(true);
$mail->Name = $_POST['name'];
$mail->Phone = $_POST['phone'];
$mail->Email = $_POST['email'];
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->send();
// echo message has been sent
// header('Location: www.***.co.za');
echo 'alert("Thank You For Contacting US, Will Get Back To You Within 24Hours")';
exit();
} catch (Exception $e) {
echo 'Failed To Send Contact Info, Please Try Again';
}
?>

GoDaddy imposes very strict (to the point of becoming almost useless) constraints on sending an email. They block outbound SMTP to ports 25, 465 and 587 to all servers except their own. This problem is the subject of many frustrating questions on Stack Overflow. The solution is extremely poorly documented by GoDaddy: you must send through their servers, and also disable all security features, username, and password (great, huh?!), giving you this config for PHPMailer:
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
GoDaddy also refuses to send with a From address belonging to any aol, gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This is because all those domains deploy SPF and DKIM anti-forgery measures, and faking your from address is forgery.
For more details:
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#godaddy

Related

PHPMailer how to set bounce email address

i know there are plenty of guides, but i tried so many of them with no result.
I also tried to reshuffle the parameters so sender goes before replyTo and vice versa, etc, but still.
The idea is to appear to the recpient as it came from something#gmail.com, but on reply (human or robot as bounce email) to always reply to noreply#custom.com
No matter what I do, the bounced email always delives to something#gmail.com instead of noreply#custom.com
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'apps/PHPMailer/src/Exception.php';
require 'apps/PHPMailer/src/PHPMailer.php';
require 'apps/PHPMailer/src/SMTP.php';
try {
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8'; // UTF8 Encoding
$mail->Encoding = 'base64'; // Needs to be set with UTF8
//$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 = "something#gmail.com"; // SMTP username
$mail->Password = "somePassword"; // SMTP password
$mail->SMTPSecure = "tls";
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Sender
$mail->setFrom('something#gmail.com');
$mail->addReplyTo('noreply#custom.com');
$mail->Sender = 'noreply#custom.com';
// Recipient
$mail->addAddress('fndsgfds#fsscd.com');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "test bouncing";
$mail->Body = "teting";
//$mail->AltBody = strip_tags($row_campaign['text']);
$mail->MessageID = $messageId; // removed from example here, but is stated above
$mail->addCustomHeader('In-Reply-To', $messageId);
$mail->send();
}
catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
die();
}
unset($mail);
Any idea where is the problem?
PHPMailer 6.1.6
The bounce email address is the SMTP MAIL FROM address, also known as the envelope sender. This is often the same as the From address (and PHPMailer uses the from address as the sender by default), but it is entirely normal for it to be different (subject to DMARC config). In PHPMailer you set it by setting the Sender property, as you are doing in this line:
$mail->Sender = 'noreply#custom.com';
When a server receives a message, it takes the envelope sender and adds it to the message in a Return-Path header; this is not something that a sending server should ever do.
So to change the bounce address, change that setting; it is mostly independent of from and reply-to addresses. Note however that gmail may ignore this if you have not configured it as an alias for your gmail username.

How to resolve SMTP could not connect error? [post STARTTLS problem]

I have installed php mailer using composer composer require phpmailer/phpmailer. I followed instructions from this website https://www.geeksforgeeks.org/how-to-send-an-email-using-phpmailer.
Below is my code for sending email.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try
{
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('owner#gmail.com', 'Owner');
$mail->addAddress('receipent#gmail.com');
$mail->isHTML(true);
$mail->Subject = "Hello this is subject";
$mail->Body = "Hello this is message;
$mail->send();
echo "Mail has been sent successfully!";
}
catch (Exception $e)
{
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
When I comment $mail->isSMTP(); and send mail, I get the result as message sent but I dont get it in my Gmail inbox.
when I uncomment $mail->isSMTP(); I get error message as shown in below image.
My project is hosted in godaddy server.
Even if I use php mail() function to send mail, response is mail send successfully, but it does not get delivered into my Gmail inbox
If you want to use Gmail, you should follow the steps here to manually add Google MX records to your GoDaddy account:
https://uk.godaddy.com/help/point-my-domains-email-service-to-google-7936
Unfortunately it takes some days for changes to be reflected.
Then you need to disable security features, change your php mailer config like:
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
Hope this solves.
------EDIT------
Later I figured out that the emails go to spam folder when the port is 25. I changed the connection to TLS and now I receive them as normal.
Here is the code from my website, it works on Godaddy, I receive e-mails to my gmail account.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'true';
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "********#gmail.com";
$mail->Password = "**********";
$mail->SetFrom("********#gmail.com");
$mail->AddAddress("********#gmail.com");
$mail->Subject = " your subject";
$mail->Body = "your body";

PHPMailer slow response time

I'm using PHPMailer to send a forgot password email to users and when the user submits their email to the code below the page takes a few minutes to load!! Im not expecting the email to be send instantly but is there a way to make it so it will redirect the user while still sending the email?
I'm fairly new to php so there could be something simple I'm missing out.
Here is my code:
<?php
define('DB_NAME', '#######');
define('DB_USER', '#######');
define('DB_PASSWORD', '#######');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
$recovery = $_POST['recovery'];
$sql = "SELECT forgotpass FROM members WHERE username = '$recovery'";
$result=mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "Please wait... ";
$mailto = $recovery;
$mailSub = "Here's your password!";
$mailMsg = $row['forgotpass'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 0;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "bemoresocial.co.uk";
$mail ->Port = 465; // or 587
$mail ->IsHTML(true);
$mail ->Username = "info#bemoresocial.co.uk";
$mail ->Password = "#########";
$mail ->SetFrom("info#bemoresocial.co.uk");
$mail ->Subject = $mailSub;
$mail ->Body = $mailMsg;
$mail ->AddAddress($mailto);
if(!$mail->Send())
{
echo "Something went wrong :(";
}
else
{
header('Location: ./success.php');
}
}
mysqli_close($conn);
SMTP can be slow, often deliberately so; it's really not well suited to sending email to remote servers during page submission processing, as PHP is typically used for.
The best way to fix this is to install a local mail server such as Postfix. Submitting messages to that (using SMTP to localhost) will be very fast, and then the mail server can deal with the slower onward delivery. This is effectively a queuing system, but is much easier than using something like beanstalkd, redis, or rabbitmq to build your own queue.
In your code, the require inside the while loop will cause a fatal error if it goes round more than once, because you will be requiring the same class file repeatedly. To send to multiple recipients efficiently, reuse the PHPMailer instance - you don't need to start from scratch every time around the loop - if you can use keepalive, this will also be much faster - see the mailing list example provided with PHPMailer for how to do that.
Though it's not part of the problem you're seeing, I can see you've based your code on an obsolete example, and you're using an old version of PHPMailer - upgrade to 6.0.
I ran into the same problem with VERY SLOW running PHP Mailer, like 3 minutes to send 15 emails using the Website Host SMTP server.
What I did to make it finish within seconds is to switch to gmail smtp. When you do this you'll need to go to Google and generate an app password Sign In Using App Password. So in your code use your normal email address with the generated password (note your normal password still works for your normal email access) and use tls security.
When I moved the program from testing to production the email failed. I found I had to go to google and just sign in and everything worked again.
Also note if you turn on SMTPDebug = 2; it will show you each step and the time it finished.
include ('class.PHPMailer.php');
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // 0=no output, 1=Commands, 2=Data & Commands, 3=2+connection status 4=Low-Level data 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 = 'mickeymouse#gmail.com'; // SMTP username
$mail->Password = 'aaaabbbbccccdddd'; // SMTP password (used google Generated app password)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
// Recipients
$i = 0; $address="";
foreach($recipients as $i => $address)
{
$mail->addAddress($address);
}
$mail->addReplyTo('mickeymouse#gmail.com', 'Mickey');
$mail->isHTML(true);
$mail->setFrom('mickeymouse#gmail.com', 'Mickey');// Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
$message = 'Message has been sent';
}
catch (Exception $e) {
$message = 'Message could not be sent.'."<br>".
'Mailer Error: ' . $mail->ErrorInfo;
}

PHPMailer - Could not authenticate

I've been running PHPMailer for a year now on a php server. Everything was fine until 3 days ago when I started getting the following error:
SMTP Error: Could not authenticate.
Allow less secure apps is ON
Here is the code:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = 1;
try {
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!=''){
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "myemailpass"; // SMTP password
$webmaster_email = "myemail#gmail.com"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
//$mail->AddReplyTo($webmaster_email, "DiFractal Customer Service");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
} catch (phpmailerException $e) {
$myfile = fopen("debug_email.txt", "w");
fwrite($myfile,$e->errorMessage() . "\n" . $mail->ErrorInfo);
fclose($myfile);//Pretty error messages from PHPMailer
} catch (Exception $e) {
$myfile = fopen("debug_email_stp.txt", "w");
fwrite($myfile,$e->getMessage());
fclose($myfile);//Pretty error messages from PHPMailer
}
}
Note I just updated PHPMailer to the latest version to try to remedy the problem but nothing has changed! The old version 5.2.2 was still having the same problem!
EDIT: I just had one successful email go through to google and sent properly. Which now makes me question if it's lag issue or something of that sort. Does anyone know how phpmailer functions under high loads or if high loads can cause the above error?
Try going to:
myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON".
Alternative:
Try changing SMTP Port to: 465 (gmail also).
I was having similar issues and needed to set the from address
$mail->setFrom('myemail#gmail.com', 'Webmaster');
Make sure you check google's usage limits! PHPMailer will not tell you particulars it will just give you the Could not authenticate error but the reason why can be because of your limits.
# https://support.google.com/a/answer/166852?hl=en
Upgraded to a new account with google business and switched to that account. Issue resolved.

PHPMailer SMTP Connection Failed - GoDaddy

I am working on a website, and in it there is a form that is used to send email through gmail with PHPMailer.
I have it all set up correctly, because it works on my AWS EC2 server. However, when I use the exact same setup on a GoDaddy hosting plan, it doesn't work (yes, I changed 'require' paths).
I am getting this error:
Mailer Error: SMTP connect() failed.
Here is my code:
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "**********#gmail.com";
$mail->Password = "*************";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->setFrom("**********#gmail.com", "Red's Mailer");
$mail->addAddress("*********#shaw.ca", "Name");
$mail->isHTML = true;
$mail->Subject = "New Submission From " . $name;
$mail->Body = $html_msg;
$mail->AltBody = $alt_msg;
Any ideas on the problem?
GoDaddy mail server does not support any email containing "FROM" header entry of aol, gmail, hotmail, yahoo, live, aim or msn.
If you are using linux cPanel hosting plan then you do need to change few lines in your php code and it will work!
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->ssl = false;
$mail->authentication = false;
$mail->addAddress("*********#shaw.ca", "Name");
$mail->isHTML = true;
$mail->Subject = "New Submission From " . $name;
$mail->Body = $html_msg;
$mail->AltBody = $alt_msg;
I had similar issues. GoDaddy does not allow SMTP outside using the emails with your domain. If you contact support your should get the same answer.
I have found out why this isn't working - GoDaddy, for some reason doesn't like letting their clients using anyone else's SMTP servers, so you either have to use the email hosting provided by cPanel, which is extremely slow and inneficient.
What I've done instead is have the script hosted on an AWS ec2 instance and have the forms for the script post to that instead.

Categories