PHP Mailer not sending SMTP mail - php

I am unable to send mail through Gmail smtp settings... On the other hand without any authentiacation i am getting mail from sg2nlhg096.shr.prod.sin2.secureserver.net server name.
SMTP settings code.
require_once "class.phpmailer.php";
require_once('class.smtp.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'abc#gmail.com';
$mail->Password = '*************';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('johndoe#gmail.com', 'John');
$mail->addReplyTo('replyto#example.com', 'Doe');
$mail->addAddress('mr.shah118#gmail.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo 0;
} else {
echo "Message sent!";
echo 1;
}
Simple Mail settings ...
require("class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail->From = $_POST['email1'];
$mail->FromName = $_POST['name1'];
$mail->addAddress("techdynastic#gmail.com", "Dynastictech Form");
//Address to which recipient will reply
$mail->addReplyTo($_POST['email1'], "Reply");
$mail->isHTML(true);
$mail->Subject = 'hidden_subject';
$mail->Body = 'body';
$mail->AltBody = "Thanks";
if (!$mail->Send()) {
//echo 'error'. mysqli_error($con);
echo 0;
} else {
//echo 'Message has beeen successfully send. we will contact as soon!';
echo 1;
}
My Html file with ajax call
$.ajax({
type: "POST",
url: "../send_form_email.php",
data: dataString,
cache: false,
success: function (data) {
//$(".success_mes").html(data);
if (data == 1)
{
$(".success_mes").html("Message has beeen successfully send.");
$('#name').val('');
$("#email").val('');
$("#phone").val('');
$("#subject").val('');
$("#comments").val('');
} else
{
$(".mail_error").html("*Something Went Wrong ..");
}
},
});

You can enable Debug to see more details:
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

Related

PHPmailer not sending messages to gmail

I am trying to send messages from my website specialeducationnotes.co.in but not able to send the messages.
<?php
if(isset($_POST['query-submit'])) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
// $mail->SMTPDebug = 4; // 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 = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$from = $_POST['email'];
$subject = $_POST['subject'];
$mail->From = $from;
$mail->FromName = $_POST['name'];
$mail->addAddress('specialeducationnotes1#gmail.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = '<h2 style="text-align: center;"s>Name: '.$_POST['name'].'<br>Email: '.$_POST['email'].'<br>Message: '.$_POST['query'].'</h2>';
$mail->AltBody = 'Hello! Hola! Namaste!';
if(!$mail->send()) {
//echo 'Message could not be sent.';
echo "<script>alert(' Your message could not be sent!');</script>";
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'Message has been sent';
echo "<script>alert(' Your message is sent!');</script>";
echo "<script>document.location.href='index.php'</script>";
}
}
?>
I just get a blank page with no error message or any text and email is also not sent. Can you tell what's wrong with my code?

mail send message display but mail is not coming [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
i have sendnewmail function using it send mail,in mail message is send message dispaly but in gmail its not coming ,this is the code
function sendnewmail($msgnew,$emailidnew,$subjectnew){
require_once("class.phpmailer.php");
require_once("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "mail.domain.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username ="user"; // SMTP username
$mail->Password ="password"; // SMTP password
$mail->From = "abcds#gmail.com";
$mail->FromName = "name";
$mail->AddAddress($emailidnew,"sudeshna");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $subjectnew;
$mail->Body = $msgnew;
if(!$mail->Send()) {
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}else{
echo 'Message send ok';
}
}
$htmldetails ="Welcome to World";
$email = "abc#gmail.com";
$subject = "Sample welcome message";
sendnewmail($htmldetails,$email,$subject);
hope this work
function sendnewmail($msgnew,$emailidnew,$subjectnew){
require_once("PHPMailer/class.phpmailer.php");
require_once("PHPMailer/class.smtp.php");
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "mail.domain.com"; // specify main and backup server
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username ="user"; // SMTP username
$mail->Password ="password"; // SMTP password
$mail->From = "abc#domain.com";
$mail->FromName = "name";
$mail->AddAddress($emailidnew,"sudeshna");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subjectnew;
$mail->Body = $msgnew;
if(!$mail->Send()) {
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}else{
echo 'Message send ok';
}
}
$htmldetails ="Welcome to World";
$email = "tausifahmad810#gmail.com";
$subject = "Sample welcome message";
sendnewmail($htmldetails,$email,$subject);

issue in sending mails in bulk in php

I am design a form for sending mail in bulk via phpMailer, for example i want to send a mail to 50 persons.but the issue is after sending mail 10 to 15 persons my form gives an file not found exception.
my ajax call is given below
.on('success.form.bv', function(e) {
e.preventDefault();
$('#send_email_next').modal({
backdrop: 'static'
});
$.ajax({
type: "POST",
url: "email_next.php",
data: $("#emailform").serialize(),
success: function(html){ $("#modal_body").html(html);}
});
});
Phpmailer function called in email_next.php is given below
require_once("class.phpmailer.php");
require_once("class.smtp.php");
$mail = new PHPMailer();
$body=" ";
set_time_limit(0);
$mail->IsSMTP();
$mail->Timeout = 10000;
$mail->SMTPKeepAlive = true;
$mail->SMTPDebug = false;
$mail->do_debug = 0;
$mail->SMTPAuth = true; // enable 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; // set the SMTP port
$mail->Username = $account_user_name; // GMAIL username
$mail->Password = $password; // GMAIL password
$mail->From = $account_user_name;
$mail->FromName = 'Rayat Bahra Group';
$mail->Subject = $_POST['subject'];
$mail->AltBody = '';
$mail->WordWrap = 50;
$mail->MsgHTML($body);
$mail->MsgHTML($content);
$mail->IsHTML(true); // send as HTML
$SN = 0;
$error_mail=0;
$total_mails_to_send = count($email_array);
foreach($email_array as $recipient_address)
{
$mail2 = clone $mail;
$mail2->MsgHTML($content);
$mail2->AddAddress(trim($recipient_address));
$status = $mail2->Send();
if($status)
{
$SN++;
}
else{
$error_mail = 1;
}
echo $SN." out of ".$total_mails_to_send. " sent <br>";
}
if($error_mail == 1)
{
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
$mail->SmtpClose();

PHP Mailer Multiple email

I have a form in my website. When the user fills out the form, I want an email to be sent to me with data entered in the form and also a thank you email for the person filling the form.
This is the code I am using:
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress('data#domain.org');
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress(''.$_POST['emailAddr'].''); // **
emailAddr is the name for email field in the form and I wish to send email to this email address.
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>

PHP Mailer SMTP ERROR

Hi I am using phpMailer For sending email. Its working fine on server, but when i am trying to send mail from my local machine(localhost) it giving me error. I am using GMAIL smtp
<?
require("lib/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "XXX#XXX.com"; // SMTP username
$mail->Password = "XXXXXXX"; // SMTP password
$mail->From = $email;
$mail->AddAddress("XXXXXX#XXX.com", "XX XX XX");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
?>
and the error is
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
It's working fine in my server.
Okay I got it in local here is the code that I used
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

Categories