Send mail with PHPMailer on iPage - php

I have trouble sending emails from my iPage webserver.
Firstly I watched a tutorial on how to use PHPMailer and the guy used gmail smpt server. That method worked flawlessly on localhost, but it doesn't work on the webserver and I cant find a solution for this anywhere. Seems like the authentication didn't work because I've got an email from google security saying someone was trying to log in to my gmail (it was 100% ipage), even accepting the new login doesn't work
If there is a possibility I'd like to send mail from my custom domain email rather than form 3rd party gmail.
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
require 'phpmailer/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = "587";
$mail->Username = ".....#gmail.com";
$mail->Password = ".....";
$mail->Subject = ".....";
$mail->setFrom('.....#gmail.com');
$mail->isHTML(true);
$mail->Body = ".....";
$mail->addAddress('.....#....');
if ($mail->send()) echo "OK";
else echo "BAD";
$mail->smtpClose();

Related

Unable to send mails from gmail smtp

I am using Gmail SMTP for sending emails in php. The code is working fine in one of my client website. When I am using the same code for another client website, I am getting an error like "Could not authenticate, SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"
. I don't have knowledge in server settings. Both websites are on different servers. Please check the below code.
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host = 'smtp.gmail.com';
//$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // for tls 587
$mail->Subject = 'Test Email';
$mail->isHTML(true);
$mail->Body = 'Message from test website';
$mail->setFrom('mahmood.bts#gmail.com');
$mail->addAddress('test#companyname.com');
$mail->addCC('info#companyname.com');
if($mail->send())
echo "mail is sent";
else
echo $mail->ErrorInfo;
?>
I used php mail function instead of SMTP, Then also I am unable to send emails. Please help me.
You should allow sending emails from an untrusted app.
Google may block sign-in attempts from some apps or devices that do
not use modern security standards. Since these apps and devices are
easier to break into, blocking them helps keep your account safe.
To disable this security feature:
Sign in to Gmail
Click here to access Less Secure App Access in My
Account.
Next to “Allow less secure apps: OFF”, select the toggle
switch to turn ON.

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.

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.

Sending mail php using gmail SMTP don`t arrives

Well, i am having an issue that i really don't have any clue and already have read everything that i could find about.
I have a code in php using phpMailer that using gmail, the emails arrives where they should when the php archive is opened by the terminal, but, when i upload to the server the emails just arrive at the final destiny if they never arrive at the mail.
in my case i want to send to an email exemple#companyName.io, that is a gmail email as well.
i just cant figure it out why the emails just work arriving at the final destiny when they are being send to a hotmail or gmail account(exemple#gmail.com) but to a exemple#companyName.io(that is a gmail as well) does not work.
Thanks in advance!
PHP code:
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; //same problem with other ports
$mail->SMTPSecure = 'tls'; //same problem with ssl
$mail->SMTPAuth = true;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
$mail->setFrom($email, $name);
$mail->addAddress('exemple#CompanyName.io', 'exemple Name');
$mail->Subject = 'Contact';
$mail->Body = $message;
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
EDIT:
i found out the answer, my server was from godaddy, so :
GoDaddy
Popular US hosting provider GoDaddy imposes very strict (to the point of becoming almost useless) constraints on sending 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. If you find your script works on your local machine, but not when you upload it to GoDaddy, this will be what's happening to you. 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 = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
GoDaddy also refuses to send with a From add
ress 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.
You may find it easier to switch to a more enlightened hosting provider.
I guess you are missing IsSMTP() setting:
$mail = new PHPMailer();
$mail->IsSMTP();

Sending Mail using PHP : SMTP ERROR

I am trying to send mail using php. But it gave me error,
" SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
"Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" "
So that I searched alot to find the problem. There I got a solution, that I need to change $mail->IsMail(); from $mail->IsSMTP();
I did it and Mail was sent...
But when I checked my mail,
I got,
"This message may not have been sent by: sender#gmail.com"
Being a developer I understood that An email should not contain such lines or issues.
I want to know, Is it alright if Receiver is showing such line in Email? and if not that What should I do?
I mean what changes I should make in my code.
Here is my php code:
**
date_default_timezone_set('Etc/UTC');
include 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// $mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "sender#gmail.com";
$mail->Password = 'senderPassword';
$mail->setFrom("sender#gmail.com", 'sender name');
$mail->addReplyTo('sender#gmail.com', '');
$mail->addAddress($receiver, '');
$mail->Subject = 'Welcome';
$mail->Body = 'body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send())
{
return "Mailer Error: " . $mail->ErrorInfo;
}
else
{
return array('flag' => "1");
}
**
isMail and isSMTP use two different sending mechanisms. isMail submits messages through the PHP mail() function, which delivers mail to a local mail server via a sendmail binary. This local mail server then tries to deliver the message to its ultimate recipient. It's possible that this local server will accept a message which is later rejected, and that will be too late for your script to know about.
With isMail:
script -> local mail server -> gmail
With isSMTP:
script -> gmail
With isMail you don't need to authenticate (localhost is usually allowed to relay), and the message is sent from your server to gmail. With isSMTP your message is sent from gmail to gmail, and it does require authentication.
When sending directly through gmail, you need to authenticate with gmail, and that has its own set of problems (that will be why your script is not working) covered thoroughly in the PHPMailer docs, examples and questions here on SO.
When sending via your server, you're saying that you are sending from a gmail user, but it's being sent by your server, not by a server listed in gmail's SPF record. This is forgery, which is why you are seeing the "This message may not have been sent by..." message. It would not say that if you sent from an address in your own domain.
The solution is to fix your gmail authentication and send directly through gmail. Base your code on the gmail example provided with PHPMailer, not the old, obsolete code you're using, and read the docs.
Here is the code which is use for mailing purpose. Try setting the SMTPDebug mode 3 and check the output.
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxxxxx';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'xxxxxxxxxxxxxxxxx';
$mail->addAddress(xxxxxxxxxxxxxx);
$mail->addReplyTo('xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx');
$mail->isHTML(true);
$mail->Subject = '';
$mail->Body = "";
$mail->send();

Categories