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
Related
All of my form works well, the only thing i,m trying to achieve is combine the first_name with last_name to give full name in the email header. I'm using PHPMailer. This is the code i tried to use -
$firstname = $_POST["first_name"];
$lastname = $_POST["last_name"];
$fullname = $firstname . ' ' . $lastname;
$fullname = $_POST["name"];
$mail = new PHPMailer(TRUE);
$mail->From = $_POST["email"]; //Sets the From email address for the
message
$mail->FromName = $_POST["name"]; //Sets the From name of the message
$mail->AddAddress('timmy2872#gmail.com'); //Adds a "To" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the
message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->AddAttachment($path); //Adds an attachment from a path on the
filesystem
$mail->Subject = 'Employer Application Form details'; //Sets the
Subject of the message
$mail->Body = $message; //An HTML or plain text message body
if($mail->Send()) //Send an Email. Return true on success or
false on error
Can anyone see what i,m doing wrong??
Fixed it eventually with this -
$full_name = $_POST["first_name"] . ' ' . $_POST["last_name"]
$mail->FromName = $full_name;
Thanks to Tim for putting me on the right track
I'm use phpmailer to sending email, I have problems when inserting the contents of the html code on the form ckeditor, but data sent to the e-mail text only.
This is my code:
require_once ('class.phpmailer.php');
$mail = new PHPMailer(true);
if (isset($_POST['btn_send']))
{
$smtp_username = strip_tags($_POST['username']);
$smtp_password = strip_tags($_POST['password']);
$ssl_port = strip_tags($_POST['port']);
$my_smtp = strip_tags($_POST['host']);
$my_ssl = strip_tags($_POST['type']);
$realname = strip_tags($_POST['realname']);
$subject = strip_tags($_POST['subject']);
$body = strip_tags($_POST['editor']);
$emaillist = strip_tags($_POST['emaillist']);
//...now get on with sending...
try
{
//$mail->isSendmail();
$mail->isSMTP();
$mail->Body = ($body);
$mail->isHTML(true);
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "$my_ssl";
$mail->Host = "$my_smtp";
$mail->Port = "$ssl_port";
$mail->AddAddress($emaillist);
$mail->Username = "$smtp_username";
$mail->Password = "$smtp_password";
$mail->SetFrom("$smtp_username", "$realname");
$mail->AddAddress($emaillist);
$mail->epriority = "3";
$mail->AddReplyTo("$smtp_username");
$mail->Subject = "$subject";
$mail->encode = ("yes");
$mail->CharSet = "utf-8";
if($mail->Send())
{
$msg = "<div class='alert alert-success'>
Hi,<br /> bro mail terkirim ke ".$emaillist."
</div>";
}
}
catch(phpmailerException $ex)
{
$msg = "<div class='alert alert-warning'>".$ex->errorMessage()."</div>";
}}
I do not know what went wrong
You need edit this row
$body = strip_tags($_POST['editor']);
to $body = $_POST['editor'];
And add this line before mail send
$mail->isHTML(true);
Function strip_tags removes html markup.
But you need filter value another way.
Please edit this line
$body = strip_tags($_POST['editor']); to $body = $_POST['editor'];
the strip_tags() function will remove all tags from your posted value. The you cant get the the html tags.
Please enable html in your PHP mailer by adding this line
$mail->isHTML(true);
So please edit the code and try this way.
This is working fine in my case.
If you are using Codeigniter, you might face a problem if doing:
$body = $this->input->post('some_field_with_html_body');
You can fix it by doing:
$body = $_POST['some_field_with_html_body'];
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;
I'm trying to use phpmailer to send out emails to each email address found in the database, but as a unique email. For some reason, it's sending duplicate emails, and it sends it out in as many copies as my query returns rows. So, if my query returns 5 rows, each recipient will receive 5 email (total emails sent is 25). I can't use the same email for multiple recipients because the email content is personalized.
What am I doing wrong with my code? Please help...
Here's my code:
$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers";
$customers = mysql_query($customers_query);
if (!$customers) {
$message = 'Error notice: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
require_once('class.phpmailer.php');
// create an instance
while ($row = mysql_fetch_array($customers)) {
$email = $row['customer_email'];
$name = $row['customers_name'];
$id = $row['customer_id'];
$mail = new PHPMailer();
// use sendmail for the mailer
$mail->IsSendmail();
$mail->SingleTo = true;
$mail->IsHTML(true);
$mail->From = "noreply#domain.com";
$mail->FromName = "MyWebsite";
$mail->Subject = "Welcome to MyWebsite";
$mail->AddAddress($email);
$mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay.";
$mail->Send();
}
So, what am missing here? Why does it send that many emails?
Try this:
$mail->ClearAddresses(); after $mail->Send();
Try something like is below, you need to be counting your rows somehow so that it doesn't re-process them. Also, the AddAddress(); function is used to keep adding email addresses to the TO: field, so you need to call ClearAddresses(); in order to restart with a fresh set of recipients.
$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers";
$customers = mysql_query($customers_query);
$count = mysql_num_rows($customers);
$result = mysql_fetch_array($customers);
$i = 0;
if (!$customers) {
$message = 'Error notice: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
require_once('class.phpmailer.php');
// create an instance
while ($i < $count) {
$email = mysql_result($result,$i,"customer_email");
$name = mysql_result($result,$i,"customers_name");
$id = mysql_result($result,$i,"customer_id");
$mail = new PHPMailer();
// use sendmail for the mailer
$mail->IsSendmail();
$mail->SingleTo = true;
$mail->IsHTML(true);
$mail->From = "noreply#domain.com";
$mail->FromName = "MyWebsite";
$mail->Subject = "Welcome to MyWebsite";
$mail->AddAddress($email);
$mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay.";
$mail->Send();
$mail->ClearAddresses();
$i++;
}
You can do one more thing, add one more extra field in the customer table, like is_email_sent, yes or no. Once sent email you can update specific row using primary key. so it will not send the same email to multiple time..
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();