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();
Related
I am getting the following error while sending the email using PHPMailer.
Error:
SMTP Error: Could not authenticate.
I am explaining my code below.
<?php
require_once('/var/www/oditek.in/subhra/phpmailer/class.phpmailer.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
I have also turned on the the less secured app option in gmail but still same error is coming. This is the implementation like contact us form in website. User will send the email request to admin(here tuurbus). Please help me to resolve this issue.
Download Phpmailer package from https://github.com/PHPMailer/PHPMailer/ link & copy into your project and extract zip folder into your project and change the your code as following also check your password ,email id. Its works by my side.
<?php
include_once('PHPMailerAutoload.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
Try:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
How can I redirect when the email is being sent with PHPMailer?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
// $mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIVSF45PCGR7NZWNQ";
$mail->Password = "Am2SBg4vluOvIc1+kycsWpCnxtf3jhGjYCAdBv7YYp/y";
//
$mail->SetFrom('test#gmail.com', 'Z-Reports'); //from (verified email address)
$mail->Subject = "Z-Reports (sent via smtp)"; //subject
//message
$body = emailZReports($total_sales, $inventory);
// $body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//
//recipient
$mail->AddAddress("test#gmail.com", "Z-Reports");
//Success
if ($mail->Send()) {
echo "Message Sent!";
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
How can I redirect to a specific page instead of displaying Message Sent!?
if you are using SMTP, you can't redirect to another page if $mail->SMTPDebug is not set to 0. This is because it sends out headers which you can't modify with header("Location: path/to/redirect"). Set $mail->SMTPDebug = 0; so that you can redirect to another page after $mail->send(). but you have to do that only when you have finished debugging and your mail is sending successfully.
You can use php header function
//Success
if ($mail->Send()) {
header("Location:Yourfile.php");//echo "Message Sent!";
}
Make sure there is not echo or any output before this function
You can use like this,
See Header for more info.
It can be like this,
//Success
if ($mail->Send()) {
header('Location: nextpage.php');
}
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.nextpage.com/"
This works
if(!$mail->Send()) {
echo "Message could not be sent. ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
} //echo getcwd();
header("Location:index.php") //redirects to a page named index.php;
// make another script for redirecting
<?php
require_once("mail_function.php");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
header("Location: form.php");exit; //redirect to form submit page
} else {
echo "Message sent!";
header("Location: index.php");exit; //redirect to your home page
}
?>
//mail_function.php
<?php
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Mailer = "smtp";
$mail->SetFrom("xxxx#gmail.com", "web");
$mail->Port = '587'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication.
$mail->Username = 'xxxx#gmail.com'; //Sets SMTP username
$mail->Password = 'xxxx'; //Sets SMTP password
$mail->SMTPSecure = 'tls'; //Sets connection prefix.
$mail->FromName = $_POST["name"];
$mail->AddAddress($email); //Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "somthing";
$mail->MsgHTML($message_body);
$mail->Body = "somthing";
$result = $mail->Send();
return $result;
?>
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
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>";
}
}
?>
I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?