SMTP Mail Not Working ON Server Working Fine On Localhost - php

Hi i am using smtp latest files and check all the solution availble on net, My SMTP php mail working on localhost not working on server my server's web mail working Fine.First of all i am inserting data into my database then those data i want to sent admin email id my data are inserting in database also mail sending working fine on localhost but given error on server it give's failed to connect with server. i contact with my hosting server support forum but they are saying this your code problem
Here is my PHP Code
<?php
if(isset($_POST['Isubmit'])){
$rname= $_POST['Iname'];
$remail= $_POST['Iemail'];
$rcontact= $_POST['Icontact'];
$rmessage= $_POST['Icomment'];
$rcourse= $_POST['Icourse'];
$date=date("Y-m-d");
$sql=mysqli_query($connect,"INSERT INTO enquiry(`NAME`,`EMAIL_ID`,`MOBILE_NO`,`COMMENT`,`SUBJECT`,`IS_DELETED`,`DATE`)VALUES('".$rname."','".$remail."','".$rcontact."','".$rmessage."','".$rcourse."','1','".$date."')");
//Mail Send Code
$ToEmail = 'admin#gmail.com';
$EmailSubject = 'Site contact form';
$MESSAGE_BODY = "Dear Sir/Mam,"."\n" ."\n"."New Contact Details are - "."\n"."\n";
$MESSAGE_BODY .= "Full Name: ".$rname."\n"."\n";
$MESSAGE_BODY .= "Email: ".$remail."\n"."\n";
$MESSAGE_BODY .= "Phone: ".$rcontact."\n"."\n";
$MESSAGE_BODY .= "Message: ".$rmessage."\n"."\n";
$MESSAGE_BODY .= "Course: ".$rcourse."\n"."\n";
//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'thave access to that date_default_timezone_set('Etc/UTC');
require 'PHPMailer-master/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)
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//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";//"lino.cspace#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";//"lino#123";
//Set who the message is to be sent from
$mail->setFrom('admin#gmail.com', 'Admin');
$mail->addAddress($ToEmail);
//Set the subject line
$mail->Subject = 'Enquiry Mail';
$mail->Body = $MESSAGE_BODY;//'this is test msg on 25-jun.';
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
}

Enable smtp debugging
$mail->SMTPDebug = 1;
OR
$mail->SMTPDebug = 2;
Then the error info will give you more info on the error
if (!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!<br>";
}

Related

PHPMailer is not sending mails

Guys I have read many answers on this question but the problem still persists.
The PHPMailer is not sending emails giving the following error.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Following is the code:
date_default_timezone_set('Etc/UTC');
require 'PHPMailer-master/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";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port =25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
//Username to use for SMTP authentication
$mail->Username = "mail#mail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom("mail#mail.com","Contact Form");
//Set an alternative reply-to address
$mail->addReplyTo($_POST['user_mail'], $_POST['user_name']);
//Set who the message is to be sent to
$mail->addAddress("mail#mail.com", "Contact Form");
//Set the subject line
$mail->Subject = $_POST['subject'] . " - Contact Form";
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("Message from ". $_POST['user_name']." : <br/>".$_POST['message'] . "<br/> Contact Details : <br/> Email :" . $_POST['user_mail'] . "<br/> Contact Number : " . $_POST['user_number']);
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<script>window.location.href='thankyou.html';</script>";
}

Sending two emails through PHPMailer simultaneously

I am trying to send two email at once using PHPMailer, one to us, and the other to the user, but for some reasons, this doesn't work. It seems I can only send one email. See below. I tried to create two complete separate email class but that still doesn't work.
// PHPmailer
//Create a new PHPMailer instance
$mail_us = new PHPMailerOAuth;
//Tell PHPMailer to use SMTP
$mail_us->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail_us->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail_us->Debugoutput = 'html';
//Set the hostname of the mail server
$mail_us->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail_us->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail_us->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail_us->SMTPAuth = true;
//Set AuthType
$mail_us->AuthType = 'XOAUTH2';
//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
$mail_us->oauthUserEmail = "jon#email.com";
//Obtained From Google Developer Console
$mail_us->oauthClientId = "";
//Obtained From Google Developer Console
$mail_us->oauthClientSecret = "";
//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
// eg: http://localhost/phpmail/get_oauth_token.php
$mail_us->oauthRefreshToken = "";
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail_us->setFrom('jon#email.com', 'Jo');
//Set who the message is to be sent to
$mail_us->addAddress("");
$mail_us->addCC('');
$mail_us->addBCC('');
$mail_us->addReplyTo('', 'John');
//Set the subject line
$mail_us->Subject = "New expert";
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail_us->Body .= "<h3>message </h3> <br>";
$mail_us->Body .= "message<br>";
$mail_us->Body .= "message <br>";
$mail_us->Body .= "message <br>";
$mail_us->Body .= "message <br>";
$mail_us->Body .= "<b>message </b><br>";
$mail_us->Body .= "message <br>";
$mail_us->Body .= "message <br>";
$mail_us->Body .= "message <br>";
$mail_us->Body .= "message <br>";
$mail_us->AltBody = 'Please view this email HTML format.';
//send the message, check for errors
if (!$mail_us->send()) {
echo "Mailer Error: " . $mail_us->ErrorInfo;
} else {
echo "Message sent!";
}
//////////////////////////////////////////////////////////////////////////
/////Email the user //////
// PHPmailer
//Create a new PHPMailer instance
$mail = new PHPMailerOAuth;
//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 = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType
$mail->AuthType = 'XOAUTH2';
//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
$mail->oauthUserEmail = "";
//Obtained From Google Developer Console
$mail->oauthClientId = "";
//Obtained From Google Developer Console
$mail->oauthClientSecret = "";
//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
// eg: http://localhost/phpmail/get_oauth_token.php
$mail->oauthRefreshToken = "";
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail->setFrom('', '');
//Set who the message is to be sent to
$mail->addAddress("");
$mail->addBCC('');
$mail->addBCC('');
$mail->addBCC('');
$mail->addReplyTo('', '');
//Set the subject line
$mail->Subject = "New expert";
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body .= "message <br><br>";
$mail->Body .= "message <br><br>";
$mail->Body .= "message<br><br>";
$mail->Body .= "message<br><br>";
$mail->Body .= "message<br><br>";
$mail->Body .= "message.<br><br>";
$mail->Body .= "Best, <br>";
$mail->Body .= "John<br>";
$mail->AltBody = "message
";
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Any help would be greatly appreciated.

Php Mailer error - Godaddy Smtp

I have a project in which I have to send 100-200 mails to users but every mail has different token for each user. I have the email hosted on godaddy.
So I was looking to send email using phpmailer. Below is the script which I am testing but it keeps giving me this 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
<?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
date_default_timezone_set('Etc/UTC');
require 'mailm/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 = 2;
$mail->DKIM_domain = '127.0.0.1';
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtpout.secureserver.net";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "itsupport#mysite.ae";
//Password to use for SMTP authentication
$mail->Password = "Mypassword";
$mail->SMTPSecure = 'ssl';
//Set who the message is to be sent from
$mail->setFrom('itsupport#mysite.ae', 'IT Helpdesk');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('sgajara#gmail.com', 'IT Helpdesk');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//Replace the plain text body with one created manually
$mail->Body = 'This is a plain-text message body';
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
This script I have found on a blog.
Any alternatives to this are also welcomed, My purpose is to send 200 emails from my godaddy email account but every email has a separate token which is fetched from the database and then inserted in the mail body and sent to the user.
That is so simple:
You must focus on smtp host, port, ssl...
Change smtp host to: relay-hosting.secureserver.net
And DELETE port and ssl, thats all...
Do not use smtp port and smtp ssl true or false...
var fromAddress = "mailfrom#yourdomain";
// any address where the email will be sending
var toAddress = "mailto#yourdomain";
//Password of your mail address
const string fromPassword = "******";
// Passing the values and make a email formate to display
string subject = TextBox1.Text.ToString();
string body = "From: " + TextBox2.Text + "\n";
body += "Email: " + TextBox3.Text + "\n";
body += "Subject: " + TextBox4.Text + "\n";
body += "Message: \n" + TextBox5.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "relay-hosting.secureserver.net";
**//Warning Delete =>//smtp.Port = 80;**
**//Warning Delete =>//smtp.EnableSsl = false;**
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
You need to modify few configurations mentioned below. let me know if this works or not.
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->ssl = false;
$mail->authentication = false;

Email containing link of my domain is not sent

I am not able to send email from any hosting containing link of my website https://www.gogglegator.com. Emails are working fine without this link and all other links in email are working fine.
When I tried phpmailer, I got error Mailer Error: SMTP Error: data not accepted.
Is there anybody who faced such issue ever and have possible troubleshooting steps? I am searching from three days but no success.
Thanks in advance for your time.
<?php
require_once('class.phpmailer.php');
include("class.smtp.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';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
//Set the hostname of the mail server
$mail->Host = "smtp.mail.yahoo.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
//Username to use for SMTP authentication
$mail->Username = "myaddress#yahoo.com";
//Password to use for SMTP authentication
$mail->Password = "mypass";
//Set who the message is to be sent from
$mail->setFrom('myaddress#yahoo.com', 'Test');
//Set an alternative reply-to address
$mail->addReplyTo('myaddress#yahoo.com', 'Test');
//Set who the message is to be sent to
$mail->addAddress('myaddress#yahoo.com');
//Set the subject line
$mail->Subject = "Test";
$mail->MsgHTML("https://www.gogglegator.com/");
if (!$mail->send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Thank you for contacting us.";
}
echo $message; ?>

I want to send email from my gmail account using php i am searching for answer on net since 4 hours but error appears

I am using wamp server and I have following code in my final.php. I am also
adding error that appears as below after code as comment kindly check that as.
I am newer to php. I am finding simple code so that I can sent message through my account, but I don't understand why this code do not have option for password.
Is password require? Please help me on this with detail and I also need to send email to multiple users simultaneously.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "63yogesh#gmail.com";
$mail->AddAddress("11ce028#charusat.edu.in");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
//The following From address failed: 63yogesh#gmail.com Message was not sent.Mailer error: The following From address failed: 63yogesh#gmail.com
SMTP server error: 5.7.0 Must issue a STARTTLS command first. jw8sm18271654pbc.73 - gsmtp
The example here says you have to set several data, such as authentication:
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
and SMTP port number:
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
EDIT:
This is the Basic Example using SMTP from their official site, try to edit it with your infos
require_once('../class.phpmailer.php'); //include("class.smtp.php"); //optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Categories