PHPMailer send multiple emails fails - php

I'm having a problem with PHPMailer. I'm creating a page for job applications for a company. There is a form, which the applicant fills, and the form data is sent to the HR. Then there must be an automatic response from the server to the applicant which thanks the application. The first email to the HR is sent, but the second is not. The code is:
$mailer = new PHPMailer();
$mailer->From = "admin#bav.hu";
$mailer->FromName = "Báv gyakornoki jelentkezés";
$mailer->AddAddress("dudas.krisztian#nerddevelopments.com", "the subject");
$mailer->isHTML(true);
$mailer->CharSet = 'UTF-8';
$mailer->Subject = "Báv gyakornoki regisztráció";
$body = "The body with the form data to HR";
$mailer->Body = $body;
$mailer->AddAttachment($cv_path, $_FILES['cv']['name']);
$mailer->AddAttachment($motivation_letter_path, $_FILES['motivation']['name']);
$success = $mailer->send();
//this email gets sent
$autoresponse = new PHPMailer();
$autoresponse->From = "gyakornok#bav.hu";
$autoresponse->CharSet = "UTF-8";
$autoresponse->AddAddress($email);
$autoresponse->Subject = "This is an automatic message, please don't answer it";
$body = "This is the automatic response to the applicant";
$autoresponse->body = $body;
$autoresponse_sent = $autoresponse->send();
//this email won't get sent

Change
$autoresponse->body = $body;
to
$autoresponse->Body = $body;

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();

How to append thread messages while reply so that new user can see previous conversation in Gmail Api

I want to send reply message from Gmail Api and it is going fine and the message is threaded or appended to the reciever mailbox (A & B user). But If I add new CC user (we name as C) then the new User should see threaded messages which was previously communicated between A & B.
Please help me out if anybody know the solution
<?php
$client = getClient();
$gmail = new Google_Service_Gmail($client);
$message = new Google_Service_Gmail_Message();
$optParam = array();
$referenceId = '';
$thread = $gmail->users_threads->get('someid#gmail.com', $threadId);
$optParam['threadId'] = '16c632fd24536690';
$threadMessages = $thread->getMessages($optParam);
$messageId = $threadMessages[0]->getId();
$subject = "Re: Thread mail test";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->From = $from_email;
$mail->FromName = $from_name;
$mail->addCustomHeader('In-Reply-To',
'<CAAdsdfsdf890sdjfklG4rJzoepBbWn3Crhq9sdfGq6kg#mail.gmail.com>');
$mail->addCustomHeader('References',
'<CAAdsdfsdf890sdjfklG4rJzoepBbWn3Crhq9sdfGq6kg#mail.gmail.com>');
$mail->addAddress($to);
$mail->addcc($cc);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$raw = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$message->setRaw($raw);
$message->setThreadId($threadId);
$response = $gmail->users_messages->send($userId, $message);
?>
Using Google Apps Script instead of Gmail API
would allow you to use message.forward which would send all the messages of a thread. You could implement it by
List all thread messages with thread.getMessages()
Get the last thread message with threadMessages[threadMessages.length-1]
Forward the last thread message to desired recipients (A, B and C) with message.forward()
If you want to stick to Gmail API, a workaround I can think of would imply to
get all thread messages
Get their raw contents
Append the raw contents of each thread message to the body of the message to send
An example:
function myFunction() {
var myThread = GmailApp.getThreadById("PASTE HERE THE THREAD ID");
var threadMessages = MyThread.getMessages();
var lastMessage = threadMessages[threadMessages.length-1];
lastMessage.forward("emailA", "emailB", "emailC");
}
The Simple way to send the Message in thread is as
1. Message Body should be like this
2. Convert message to base64 encode.
3. Use https://developers.google.com/gmail/api/v1/reference/users/messages/send to send an email using thread-id.
4.enjoy coding.

Yii send email to multiple recipients

hello im using yiimail here. i want to sent an email to multiple recipients
here is my code
$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
// $mail->setTo(array($emailReceiver));
$mail->setTo($model->receiver1);
$mail->setCC($mailCC);
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);
$mailCC get input value from user and $model->receiver1 from database. if user input 2 other user for $mailCC, this only send to the first email, not both.
ex:
$model->receiver1=email1#mail.com
$mailCC = array("email2#mail.com", "email3#mail.com") //this is from user input
the email will only send to email1#mail.com & email2#mail.com
i've tried
$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
// $mail->setTo(array($emailReceiver));
$mail->setTo($model->receiver1);
$mail->setCC(array($mailCC)); //this one with array
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);
but it return this error
preg_match() expects parameter 2 to be string, array given
where did i do wrong?
You can try this
$mail->addCC($mailCC[0]);
$mail->addCC($mailCC[1]);
Hopefully it will work. Here is my complete running code
$message = new YiiMailMessage;
$message->view = 'registrationFollowup';
//userModel is passed to the view
$message->setBody(['userModel' => "test"], 'text/html');
$message->setTo("xxxx#gmail.com");
$message->addCC('yyyy#gmai.com');
$message->addCC('zzz#gmai.com');
$message->from = "pqr#gmail.com";
$status = Yii::app()->mail->send($message);
print_r($status); //print the number of recipient, which 3
i got this problem done
here's my code
$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
foreach($mailcc as $to){
$mail->addTo(trim($to));
}
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);
we should explode separator, loop recipients and change to addTo not setTo
thanks to u guys that have give some solution

PHPMail - can I send two unique emails

I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}

PHPMailer not sending mail

I'm trying to send an order confirmation and also notify the seller about a user purchase. However, PHPMailer only sends the first email. Here's quick and dirty:
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Subject';
$email->Body = $bodytext;
$email->AddAddress($_REQUEST['sahkoposti']);
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
<?php
//send message to seller
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Tilaus vastaanotettu';
$email->Body = $bodytext;
$email->AddAddress("mail#mail.com");
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
Is it even possible to send multiple emails from one script?
It is possible, however you're using return in the first statement, which will stop execution of the function. Remove the first return (just use $email->Send();) and it should work.
The second email does not get executed because you are returning right after sending the first email, you should change:
return $email->Send();
for this:
$email->Send();

Categories