Send multiple mail is not happening - php

Thanks in advance to all. I am designing a shopping cart in php and mysql. I want to send order confirmation email to the client and also to admin registered email-id. My email function is
function send_mail($address,$subject,$message,$path){
global $error;
$mail = new PHPMailer();
//$mail->isSMTP();
//$mail->SMTPDebug = 1; // Enable verbose debug output
//$mail->SMTPAuth = true;
//$mail->SMTPSecure ='ssl' ;
//$mail->Host ="smtp.mail.yahoo.com";
//$mail->Port = '465'; // use 587 also
//$mail->Username = "";
//$mail->Password = "";
$mail->setFrom("sales#dressncrafts.com", "Dress N Crafts");
$mail->addReplyTo("sales#dressncrafts.com", "Dress N Crafts");
$mail->addAddress($address); // Add a recipient
$mail->Subject = $subject;
//$mail->AltBody = $message; // optional, comment out and test
$mail->MsgHTML($message);
$mail->addAttachment($path); // attachment
$send = $mail->send();
$mail->clearAddresses();
$mail->clearAttachments();
if(!$send) {
return FALSE;
} else {
return TRUE;
}
}
Now the problem is when I triggered the function to send mail, only customer email is sent but admin email is not sending. I have tried to input sleep function, use the same function in a different name, but all gone in vain. The absurd thing is that if I changed the order of the mail, only customer mail is sending. Please help me to solve the problem. thank you. the very code is
$message = "Hello $name, your order (order number:$ordernumber) has been
placed on the date $date with the following details."
.'<br><br><center>'.$body.'</center><br>'."Subtotal:Rs.
$subtotal".'<br>'."Delivery Charges:Extra".'<br>'."Total:Rs.
$total".'<br>'.
"Your order is under process and the invoice is attached with the
mail.Please find it.".'<br><br>'.
"Thanking you".'<br>'.$about->getShopname();
$subject = "Successful order";
$cust_mail=send_mail($email,$subject,$message,$invpath);
$shopsubject = "New Order";
$shop_email= $about->getEmail();
$shop_mail= send_mail($shop_email,$shopsubject,"New order has arrived, find
the attachment",$invpath);
if($shop_mail==TRUE && $cust_mail==TRUE){
foreach($cartresult as $cres)
{
$cart->setIdcart($cres->idcart);
$cart->deleteCart();
}
redirect("../checkout.php?plsord=TRUE&cust={$cust_mail}&shop=
{$shop_mail}");
}
else {
foreach($cartresult as $cres)
{
$cart->setIdcart($cres->idcart);
$cart->deleteCart();
}
redirect("../checkout.php?plsord=TRUE&some=false&cust=
{$cust_mail}&shop={$shop_mail}");
}

Related

PHPMailer sometimes send mails sometimes not.use gmail smtp

i am using phpmailerclass with my website forms and its working 100% fine and i could not see any error also sending emails fine but sometimes rare miss send email. when form miss send email behaviour is like when click on submit button miss send email but on second try click submit send email. not always like that email send perfect on first try but when problem start need to second try then send email. i have Gsuite business account google side setting correct too. why sometimes missing ?
include("phpmailer/PHPMailerAutoload.php");
$fmail="username#domain.com";
// start email1
$mail = new PHPMailer();
$mail->Host='smtp.gmail.com';
$mail->port=587;
$mail->SMTPSecure='tls';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "username#domain.com";
$mail->Password = "passwordhere";
$mail->FromName = "companyName";
$mail->SetFrom($fmail);
$mail->AddAddress($mailaddress);
//$mail->AddCC("companyname#gmailcom.com");
$filename11=$fileName1;
if($filename11!="")
{
$dir1= uploadFilePathAdmin.$orderNo."-".$filename11;
$mail->AddAttachment($dir1);
}
$filename12=$fileName2;
if($filename12!="")
{
$dir2= uploadFilePathAdmin.$orderNo."-".$filename12;
$mail->AddAttachment($dir2);
}
$mail->IsHTML(true);
$mail->Subject = "".stripslashes($designName)."-".$orderNo." - PO: ".$ponumber."";
$mail->Body = $MESSAGE;
$mail->AltBody = $MESSAGE;
$mail->Send();
// start email2
$mail2 = new PHPMailer();
$mail2->Host='smtp.gmail.com';
$mail2->port=587;
$mail2->SMTPSecure='tls';
$mail2->IsSMTP();
$mail2->SMTPAuth = true;
$mail2->Username = "username#domain.com";
$mail2->Password = "passwordhere";
$mail2->FromName = "companyName";
$mail2->SetFrom($emailee);
$mail2->AddAddress($fmail);
$mail2->AddReplyTo($mailaddress);
$filename21=$fileName1;
if($filename21!="")
{
$dir33= uploadFilePathAdmin.$orderNo."-".$filename21;
$mail2->AddAttachment($dir33);
}
$filename22=$fileName2;
if($filename22!="")
{
$dir22= uploadFilePathAdmin.$orderNo."-".$filename22;
$mail2->AddAttachment($dir22);
}
$mail2->IsHTML(true);
$mail2->Subject = "".$designName."-".$orderNo." - PO: ".$ponumber."";
$mail2->Body = $MESSAGE;
$mail2->AltBody = $MESSAGE;
$mail2->Send();

Trying to create a php auto email after a form has been submitted, and style the email with HTML

I'm building a form that allows someone to sign up for a deal. I want the form to send 3 emails using php once it has been submitted; one sending the details of the form to myself, one sending the details to the person offering the deal, and one sending the actual deal confirmation and voucher to the person who signed up.
I've run into problems in that certain email addresses don't seem to receive or be sent the email when i'm testing it (it works for both my hotmail and gmail addresses, but not for my own domain email address). I also can't figure out how to add some sort of html style to the emails being sent.
How can I style these emails and make sure that they are being sent to right email addresses?
here is the code:
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$airline=$_REQUEST['airline'];
$position=$_REQUEST['position'];
$checkin=$_REQUEST['checkin'];
$attendance=$_REQUEST['attendance'];
$email=$_REQUEST['email'];
$terms=$_REQUEST['terms'];
if (($name=="")||($position=="")||($checkin=="")||($attendance=="")||($email==""))
{
echo "All fields are required, please fill the form again.";
}
//email going to me//
else{
$from="From: Deal 1 Form Submission<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemail#test.com",
$subject,
$message="someones taken the deal: heres their info: <br> Name: $name\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n" );
echo "Email sent!";
}
//email going to the customer//
{
$from="From: me<myemail#test.com>\r\nReturn-path: $email";
$subject="Thank you for choosing this deal";
mail("$email", $subject, $message="thanks for choosing this deal!, please present this voucher when attending the restaurant", $from );
}
//email going to the partner//
{
$from="From: Me<myemail#test.com>\r\nReturn-path: $email";
$subject=" Great news! Someone has chosen your deal";
mail("partneremail#test.com",
$subject,
$message="Fantastic news, a customer has taken your deal! here's their info: Name: $name\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n
", $from );
}
}
?>
Many thanks.
Your best bet is to use PHPMailer. Here is some example code that relays the email through gmail. The reason why you would want to do this is to avoid emails from the server going into you spam box.
$debug = true; // set it to false in production
$Mail = new PHPMailer();
if ($debug) {
// this allows you to see the details of the connection to gmail
$Mail->SMTPDebug = 4;
}
$Mail->isSMTP();
$Mail->Host = 'smtp.gmail.com';
$Mail->SMTPAuth = true;
$Mail->Username = 'your#gmail.com';
$Mail->Password = '[your-password]';
$Mail->SMTPSecure = 'tls';
$Mail->Port = 587;
$Mail->setFrom('from#address.com', 'John Smith');
$Mail->addAddress('to#address.com', 'Someone Else');
$Mail->addReplyTo('from#address.com', 'John Smith');
$Mail->addBCC('bcc#address.com', 'BCC Person');
$Mail->addAttachment('path/to/img/or/pdf/or/whatever/you/want/to/attach.pdf');
$Mail->Subject = 'Test Subject';
$Mail->Body = '<h1>Hi! This is an HTML format body'
$Mail->AltBody = 'Hi! Im just a text email in case HTML is not supported!';
if (!$Mail->send()) {
print $Mail->ErrorInfo;
return false;
} else {
return true;
}

phpmailer not sending email to gmail,yahoo,hotmail or these are blocking email sent by phpmailer

i am using PHPmailer to send emails
here is code that i have used:
$mail = new PHPMailer();
$subject = "test";
$to = "test_patel#yahoo.com"
$mail->SetFrom("PDSociety#aol.com","Punjab Dental Society");
$mail->AddReplyTo("PDSociety#aol.com", "Punjab Dental Society");
$mail->Subject = $subject;
$mail->MsgHTML($str);
$mail->AddAddress($to, "Punjab Dental Society");
if(!$mail->Send())
{
$err = "Mailer Error: " . $mail->ErrorInfo;
//echo $err;
} else {
$msg = "Message sent!";
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
if i change email address from yahoo to gmail or hotmail, still email are not sent.
i checked by echoing error, but no errors.
can anyone explain what is the issue ?
After trying various ways, i found following code working with almost all email providers
$to['email'] = "recipients email address";
$to['name'] = "name";
$subject = "email subject";
$str = "<p>Hello, World</p>";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'Specify main and backup server here';
$mail->Port = 465;
$mail->Username = 'xyz#domainname.com';
$mail->Password = 'email account password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'From Email Address';
$mail->FromName = "Any Name";
$mail->AddReplyTo('xyz#domainname.com', 'any name');
$mail->AddAddress($to['email'],$to['name']);
$mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $str;
if(!$mail->Send()) {
$err = 'Message could not be sent.';
$err .= 'Mailer Error: ' . $mail->ErrorInfo;
}
$mail->ClearAddresses();
variable values needs to be changed accordingly.
Hope these helps people having issues with PHPmailer
PHPMailer is only involved in submitting the message to your own mail server, and you're not having any problem there. After that, your mail server takes on the responsibility of sending it on, so you will find the answer in your mail server's logs.
There is no simple way to ensure messages end up in the inbox and not spam - if there was, spammers would be using it and filtering would be useless. Make sure your DNS resolves backwards and forwards, that you have valid SPF records, that you sign your messages with DKIM (especially important for Yahoo) and most importantly, that you don't send messages that your recipients think are spam.
Try this :
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->AddAddress($to['email'],$to['name']);
$mail->FromName = '';
$mail->Subject = $subject;
$mail->MsgHTML($message);
$send = true;
return $mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
$e->getMessage(); //Boring error messages from anything else!
}
It will help you if any exception error.
Have you looked on the post here: Using PHPMailer Results in many blocked emails? The asker solved the issue by changing the email subject:
Well I solved the issue; the code above was not the problem and works
great.
In my subject, I used a phrase regarding "verify your account
information" and that got it blocked on a few ISP's.
So the lesson is, your subject matters. I was looking at my php code
and my body content before I realized this.
The content of the email and its subject can make ISPs ban it. You could try taking the content of one of your received emails from your inbox and see if that goes through.

How can I add a PHP script to Auto-respond with a "thank you" message

I'm php newbie that just figured out how to use php with phpmailer to send email addresses of my users to my email address to be added to a newsletter.
However, now I want to add a simple auto-respond script in php, so when users add their email to my guestlist it sends them an autoreply email to their email that says:
Thanks for signing up. [Picture of my logo] www.mysite.com
I've searched and searched, but I haven't been able to find a proper answer on how to create an autorespond script in php. Please let me know how I can accomplish this task. Thank you!
<?php
$email = $_REQUEST['email'] ;
require("C:/inetpub/mysite.com/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
$mail->Host = "smtp.comcast.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myusername#comcast.net"; // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("myemail#gmail.com", "Guestlist");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "A new member wishes to be added";
$message = $_REQUEST['message'] ;
$mail->Body = $email;
$mail->AltBody = $email;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
$mail2 = new PHPMailer();
// set mailer to use SMTP
$mail2->IsSMTP();
$mail2->Host = "smtp.comcast.net"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = "myusername#comcast.net"; // SMTP username
$mail2->Password = "*******"; // SMTP password
$mail2->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail2->Port = 25;
$mail2->From = $email;
// below we want to set the email address we will be sending our email to.
$mail2->AddAddress("$email");
// set word wrap to 50 characters
$mail2->WordWrap = 50;
// set email format to HTML
$mail2->IsHTML(true);
$mail2->Subject = "Thank you for joining";
$message = "Please stay tune for updates" ;
$message = $_REQUEST['message'] ;
$mail2->Body = $message;
$mail2->AltBody = $message;
if(!$mail2->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail2->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Update: 1
Ok, I figured out how to send auto-respond emails to users. However, now the users are receiving messages with their own email address and the name Root user
So what can I do to fix this problem so that users see my email address when they recevie auto-responses, and how can I make sure it says my name instead of root user?
Do not use the submitter's email address as the from address - it won't work as it looks like a forgery and will fail SPF checks. Put your own address as the From address, and add the submitter's address as a reply-to.
Using SMTPSecure = 'ssl' with Port = 25 is an extremely unusual combination, and very likely to be wrong. ssl/465 and tls/587 are more usual.
To send multiple messages, you do not need to create a second instance - just re-use the same one. You can reset any individual properties you want (such as Body) and clear addresses using $mail->clearAddresses(), then just call $mail->send() a second time.
It looks like you have based your code on an old example (try a new one) - make sure you are using latest PHPMailer - at least 5.2.10.
add:
echo "Message has been sent";
$mail = new PHPMailer();
$mail->From = 'myemail#gmail.com';
// below we want to set the email address we will be sending our email to.
$mail->AddAddress($email);
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "Thanks for joining ...";
$message = "Thanks for joining...";
$mail->Body = $email;
$mail->AltBody = $email;
$mail->Send();

Add a CC to a mail to script

I have a form that sends an email and I want to add a cc email to the mail to line. Please show me how I would need to have the line below to acomplish this. Thanks
mail("mainemail#email.com;CCemail#email.com", $subject, $message, $header);
public function SMTPClient($subject,$content,$instance) {
try {
$mailer = new PHPMailer(true);
$mailer->IsSMTP();
$mailer->SMTPDebug = 1;
$mailer->SMTPAuth = def_SMTP_AUTHENTICATION; // enable SMTP authentication
$mailer->SMTPSecure = def_SMTP_SECURE; // sets the prefix to the servier
$mailer->Host = def_SMTP_SERVER; // sets GMAIL as the SMTP server
$mailer->Port = def_SMTP_PORT; // set the SMTP port for the GMAIL server
$mailer->Username = def_SMTP_USERNAME; // GMAIL username
$mailer->Password = def_SMTP_PASSWORD; // GMAIL password
$mailer->SetFrom(def_MAIL_FROM_ADDRESS, def_MAIL_FROM_NAME);
$to = def_MAIL_TO;
$mailer->AddAddress($to);
$mailer->Subject = $subject;
$mailer->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mailer->WordWrap = 80; // set word wrap
$mailer->AddCC('xzy#gmail.com', 'xyz');
$mailer->AddReplyTo(def_MAIL_TO, 'abc');
$mailer->MsgHTML($content);
$mailer->IsHTML(true); // send as HTML
$mailer->Send();
$this->logger("Message has been sent Successfully","INFO",$instance);
echo 'Message has been sent Successfully.';
} catch (phpmailerException $e) {
echo $e->errorMessage()`enter code here`;
}
}
Note:Use the mailer component from http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/detail?name=PHPMailer_5.2.4.zip&can=2&q=

Categories