PHPMailer HELP - Getting SMTP error but mail is send - forms

I'm ripping my head off in a moment...
Using PHPmailer to send a email from my site.
I have created a HTML-form on my website, and the values from there needs to go to my mail... You know - standard :)
But I keep getting this error :
Mailer Error: SMTP connect() failed.
When I have turned SMTPDEBUG on it goes like this:
SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.
The host and port is correct, got the details from my provider..
Is there something i'm missing, typed in wrong or misunderstood?
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// here we use the php mail function
// to send an email to:
// you#yourdomain.com
mail( "info#recive.com", "Feedback Form Results",$message, "From: $email" );
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.test.com'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#recive.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->From = 'info#recive.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->addAddress('info#recive.com'); // Name is optional
//$mail->addReplyTo('info#recive.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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;
exit;
}
echo 'Message has been sent';
?>

From my understanding the 'ssl' setting for SMTPSecure is for direct ssl connect, while the 'tls' setting is for plain connect followed by upgrading to SSL with the STARTTLS command. With port 25 you need plain connect, e.g. 'tls'.

Related

Not able to get data when form is submitted using phpmailer and later forwared

Simple PHP script to capture user form input and send me email.
Empty form gets submitted using phpmailer script i.e. Receiving email without user filled data. There is no SMTP error, Page get submitted and redirected to new Page. Request to please help. Sometimes email received also form has user filled data also, not sure why ?
<?php
date_default_timezone_set('Etc/UTC');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';
try {
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "XXXXX#gmail.com";
$mail->Password = "XXXXX";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXX#gmail.com', 'Mr Singh');
$mail->addReplyTo('XXXXX#gmail.com', 'Mr Singh');
$mail->addAddress('XXXXX#gmail.com', 'Mr Singh');
$mail->Subject = 'PHPMailer Exceptions test';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['name']}
Email: {$_POST['email']}
Mobile: {$_POST['mobile']}
EOT;
$mail->send();
header("Refresh:1; contact.html");
} catch (Exception $e) {
echo $e->errorMessage();
} catch (\Exception $e) { //The leading slash means the Global PHP Exception class will be caught
echo $e->getMessage();
}
?>
If this script is ever run at all, it will send an email, regardless of whether any data was submitted or not. Wrap it in a condition that only sends a message if data has been submitted, something like:
if (isset(_POST['email'])) {
try {
$mail = new PHPMailer(true);
...
You can be more thorough about this check, for example validate that a valid email address was submitted, that the other fields contain what you expect, and so on, but you should get the general idea.
Thanks, Kept only if statement and its working now. Able to use use Gmail SMTP services. For Gmail had to enable less secure app in Gmail and changed password once.
<?php
date_default_timezone_set('Etc/UTC');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';
if (isset($_POST['submit']))
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "XXXXX#gmail.com"; // Your Gmail address.
$mail->Password = "XXXXX"; // Your Gmail login password or App Specific Password.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
//Set who the message is to be sent from
$mail->setFrom('XXXXXgmail.com', 'Mr Singh');
//Set an alternative reply-to address
$mail->addReplyTo('XXXXX#gmail.com', 'Mr Singh');
//Set who the message is to be sent to
$mail->addAddress('YYYYY#gmail.com', 'Mr Singh');
$mail->Subject = 'Customer Enquiry Form';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['name']}
Email: {$_POST['email']}
Mobile: {$_POST['mobile']}
EOT;
$mail->send();
header("Refresh:1; contact.html");
} else {
echo "Your form is not submitted yet please call for booking";
}
?>
HTML Code
<form class="form-horizontal" id="submit" action="PHPMailer.php" method="post">
....... ** HTML CODE of Form .......
<input value="submit" name="submit" type="submit">

phpmailer script on godaddy is not working [duplicate]

This question already has answers here:
PHPMailer GoDaddy Server SMTP Connection Refused
(21 answers)
Closed 5 years ago.
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0) 2017-11-15 13:11:07 SMTP connect() failed.
<?php
require 'PHPMailerAutoload.php';
//echo !extension_loaded('openssl')?"Not Available":"Available <br/>";
$email = $_POST['email'];
$number = $_POST['phone'];
$client = $_POST['client'];
$to = 'myemailid#gmail.com';
$subject = 'user registration';
$phone = "phone number:".$number;
$message = "client details:"."\n"."email:".$email."\n"."phone number:".$number."\n"."client:".$client;
$headers = "From:".$email;
$mail = new PHPMailer;
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'hosting.secureserver.net'; // ssl://smtp.gmail.com // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->From = 'myemailid#gmail.com';
$mail->SMTPSecure = 'none'; //TLS // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; //587 // TCP port to connect to
$mail->setFrom($email);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()) {
header("Location: ../../thankyou.html");
}
else {
echo $mail->ErrorInfo;
//header("Location: ../../error.html");
}
?>
this is the error i am receiving, as i am new to this php any detailed explanation would be highly appreciated. thank you
The host hosting.secureserver.net does not exist. You need to set the correct server and credentials for the outbound SMTP server. This information should be provided by whoever is hosting your email services - probably your web host.

Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
// $mail->SMTPDebug = 2; // 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 = 'eportalforresidency#gmail.com'; // SMTP username
$mail->Password = '##RESIDENCY##'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->SMTPSecure = 'ssl';
$mail->setFrom('eportalforresidency#gmail.com', 'naimish golakiya');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('eportalforresidency#gmail.com'); // Name is optional
$mail->addReplyTo('eportalforresidency#gmail.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';
}
?>
i am trying to send a mail through phpmailer but its not working anyone could please help.i have downloaded te phpmaile from gitub and i have also tried to slove this error amonggg the amsweres mentioned in stackoverflow but still its not working.
Update the port number to 587. Also, those are fake credentials right?

PHPmailer: SMTP connect() failed (not working)

I want to send mail using phpmailer from localhost XAMPP using SMTP server (gmail). But I keep getting this error:
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I tried many solutions regarding uncomment openSSL in php.ini file, changing the port 465("ssl") and 587("tls"), but it does not work.
My codes:
<?php
date_default_timezone_set('Etc/UTC');
'PHPMailerAutoLoad.php';
class.phpmailer.php if not already loaded
$port =465;
$securetype = 'ssl';
$from = 'myemail#gmail.com';
$name = 'User';
$toemail= "mymail#gmail.com";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->isSMTPDebug = 1;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = $securetype;
$mail->Port = $port;
$mail->From = $from;
$mail->FromName = $name;
$mail->addAddress($toemail);
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
You will need class.phpmailer.php. If you're using SMTP, you'll need class.smtp.php.
Try this demo code:
<?php
require 'class.phpmailer.php';
require 'class.smtp.php';
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
#require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'mail.domain.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, a.k.a. RFC4409 SMTP submission
$mail->Port = 25;
//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 = "demo#domain.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('demo#domain.com', 'subillion');
//Set an alternative reply-to address
#$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress("example#gmail.com");
//Set the subject line
$mail->Subject = 'Demo !!';
//Read an HTML message body from an external file, convert referenced images to embedded,
$mail->isHTML(true);
$msgbody = "This is a demo test !";
$mail->Body = $msgbody;
//send the message, check for errors
if (!$mail->send()) {
// echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>

issue of Same sender and receiver but different ReplyTo in Gmail

Let's take example that I have three Email addresses.
-testing1#gmail.com
-testing2#gmail.com
-testing3#gmail.com
I want to set reply to field to testing3#gmail.com always.
If i send an email from testing1#gmail.com to testing2#gmail.com then it's working.
But If send an email from testing1#gmail.com to testing1#gmail.com and set reply to field to testing3#gmail.com then it's not working.
In an popup it shows reply to as testing3#gmail.com.But If I press on reply button it automatically changes to testing1#gmail.com.
I don't know why this is happening. has this happened with anyone before ?
EDIT
Actually this is just a problem of Gmail. In Gmail it doesn't work but it works in yahoo and hotmail. I don't know that is this a bug of Gmail or they have designed it this way.
Edit 2
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test1'; // SMTP username
$mail->Password = 'PASSWORD'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = ''test1#gmail.com";
$mail->AddAddress('test1#gmail.com', $name); // Add a recipient
$mail->AddReplyTo('list123#gamil.com', 'List manager');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$message = "Hi Hello" ;
$mail->Subject = 'Subject;
$mail->Body = $message;
$mail->Send();
Note
I am giving same address in sender and receiver both. But giving different replyTo.
If you use smtp.gmail.com to send a message, and the sender's email address is not yourgoogleemailname#gmail.com, then Gmail will rewrite the headers and set the from address to yourgoogleemailname#gmail.com. See http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server for more info, and for a possible solution.