PHPMailer works fine on localhost but not on live server [duplicate] - php

I've got a bizarre problem here. I'm trying to use PHPMailer to send an email, through SMTP. I have a website hosted by GoDaddy and it's that SMTP account that I'm trying to use to send the mail.
It works if I execute my PHP file on my localhost server.
It does not work if I execute my PHP file on GoDaddy's server.
The error message I get is:
SMTP -> ERROR: Failed to connect to server: Connection refused (111)
I checked phpinfo on both localhost and the remote server. Both have smtp_port listed as 25. I'm using WAMP on my machine and the server is some form of Linux (which I know nothing about and have no idea how to administer).
Here is the code in question:
INDEX.PHP:
<?php
date_default_timezone_set('America/Los_Angeles');
include_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer;
$mail->SMTPDebug = 1;
$mail->Port = 25;
$mail->IsSMTP();
$mail->Host = 'smtpout.secureserver.net';
$mail->SMTPAuth = true;
$mail->Username = 'username#site.com';
$mail->Password = 'super_secret_password';
$mail->SMTPSecure = ''; // tried ssl and tls, with same result
$mail->ClearAddresses();
$mail->AddAddress('receiver#hotmail.com', 'Receiver Name');
$mail->From = "username#site.com";
$mail->FromName = "Username";
$mail->Subject = 'Hi there';
$mail->Body = "This is a message";
if ($mail->Send()) {
echo "Message sent!\n";
}
else {
echo "Message failed!\n";
print_r($mail->ErrorInfo);
}
exit();
?>

I think you should perform two step
1) check your port as suggested on godaddy support http://support.godaddy.com/help/article/319/what-do-i-do-if-i-have-trouble-connecting-to-my-email-account
2)use "relay-hosting.secureserver.net" as your host instead of "smtpout.secureserver.net"
GoDaddy does allow to send emails using Gmail as your SMTP, just need to get rid of the smtp.gmail.com and use their Host instead. This is my setup:
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "relay-hosting.secureserver.net";
$mail->Username = "your-account#gmail.com";
$mail->Password = "yourpassword";
// ...
// send from, send to, body, etc...
Reference (see first two posts) http://support.godaddy.com/groups/web-hosting/forum/topic/phpmailer-with-godaddy-smtp-email-server-script-working/

If your hosting has own email server, the server will use the following ports 25,465,587.
Settings for GoDaddy:
$mail->isSMTP();
$mail->Host = localhost;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
//$mail->SMTPSecure = 'tls';
//$mail->Port = 587;
For other providers you have to create a mailbox with your domain:
$mail->isSMTP();
$mail->Host = localhost;
$mail->SMTPAuth = true;
$mail->Username = 'example#yourdomain.com';
$mail->Password = 'password';
//$mail->SMTPSecure = 'tls';
//$mail->Port = 587;

Related

How to connect and authenticate on the STMP server

I need to connect to the GMAIL SMTP server with email and password, without sending email and then receiving 200 OK.
I using the code PHPMailer:
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myuser#gmail.com";
$mail->Password = "password";
$mail->SetFrom("");
$mail->Subject = utf8_decode("My subject");
$mail->Body = "hello";
$mail->AddAddress("destination#gmail.com");
$mail->Body = template('emails/reset-password', compact('comentario'));
if ($mail->Send()) {
$mail->ClearAllRecipients();
$mail->ClearAttachments();
}
My script is sending email, I would just like to validate the authentication.
PHPMailer itself isn't much use for this as it's all about the sending. However, the SMTP class that's part of PHPMailer can do this just fine, and there is a script provided with PHPMailer that does exactly as you ask. I'm not going to reproduce the script here as it just creates maintenance for me!
The script connects to an SMTP server, starts TLS, and authenticates, and that's all.

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";

SMTP ERROR: Failed to connect to server: Connection refused (111) on Godaddy server

I want to send mail using phpmailer. I have uploaded my phpmailer files on my Godaddy server. The below code is running in my localhost but not on my server.
2019-02-03 16:54:12 SMTP ERROR: Failed to connect to server: Connection refused (111)
The below is the code for php
<?php
include_once('PHPMailer/src/PHPMailer.php');
include_once('PHPMailer/src/SMTP.php');
$mail = new PHPMailer\PHPMailer\PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587,465
$mail->IsHTML(true);
$mail->Username = "mail#gmail.com";
$mail->Password = "password";
$mail->SetFrom("mail#gmail.com");
$mail->Subject = "Test mail";
$mail->Body = "Hello World";
$mail->AddAddress("mail#gmail.com");
if($mail->Send()) {
echo "Message has been sent";
}
?>
use PHPMailer\PHPMailer\PHPMailer; <-- make sure these are not in a function
use PHPMailer\PHPMailer\Exception;
require 'path/src/Exception.php';
require 'path/src/PHPMailer.php';
require 'path/src/SMTP.php';
Use tls and not ssl it can be buggy
$mail->SMTPSecure = "tls";
$mail->Port = 587;
Check out my answer here this will tell you everything you need to know to connect to gmail with phpmailer

How to configure SMTP settings based on web hosting's mail account

Hello how do i configure my php mailer to make it work with these settings?
i tried to fix the username and password but i still get this error
PHPMailer Error: The following From address failed: thesis#thesis.buybranded.com.ph : Called Mail() without being connected
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'gowebph.gowebph.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
// or try these settings (worked on XAMPP and WAMP):
// $mail->Port = 465;
// $mail->SMTPSecure = 'ssl';
$mail->Username = "thesis#thesis.buybranded.com.ph";
$mail->Password = "XXXXXXXXXXXXXX";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
$mail->From = "thesis#thesis.buybranded.com.ph";
$mail->FromName = "Your Name";
$mail->addAddress("vince_agno#live.com","User 1");
$mail->addAddress("2009010067#ust-ics.mygbiz.com","User 2");
$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
You've got your port & protocol mismatched. port 465 is for ssl and port 587 is for tls.
see https://support.google.com/mail/answer/13287?hl=en
Could you please use this
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
also
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
Or if using TLS try to use port 587 for TLS
from wiki ---Secure SMTP - port 587 (can also use the legacy port 465 - this may solve problems with SSL) (port 587 has optional TLS encryption, possibly using STARTTLS now, or use port 465 for SSL encryption)

PHP Contact Form with Network Solutions

Have a basic contact form using https://github.com/PHPMailer/PHPMailer on a Network Solutions shared hosting package.
// start the mail request basics
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->Debugoutput = 'html';
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = 'my_gmail_account#gmail.com';
$mail->Password = '*******';
Everything works find locally (on my computer) and form sends email, but when I upload this php file to our network solutions hosting I get the following Error.
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Is there something wrong with my Mail Settings? Anyone else had success with PHP mailing on Network Solutions Shared Hosting?
Network Solutions blocks several ports required to send mail including those needed for gmail.
If anyone finds an alternate, please let me know.
For now, used a dummy account and used standard PHP mailer then forwarded mail.
You could change your config options like this.
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "my_gmail_account#gmail.com";
$mail->Password = "********";
$mail->SetFrom("somemail#gmail.com");
$mail->Subject = "Testing the New Config";
$mail->Body = "Hey there !";
I got it to work with these settings:
$mail->SMTPOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
)
);
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->Host = "email-out-priv.myregisteredsite.com";
$mail->Port = 25;
$mail->Username = "noreply#mydomain.com";
$mail->Password = "********";
You have to pay for an email address with Network Solutions for a domain you own. You can see the host and port by logging into your mailbox (mail.mydomain.com) --> Top right Menu --> Settings --> Mail --> Mail and Social Accounts --> E-Mail (Edit) --> Outgoing server (SMTP). I used this PHP Mailer Documentation as a clue to this solution.

Categories