yii mailer extension: how to send multiple email addresses at once
-
30-06-2021 - |
Frage
Q : How can I send multiple addresses at once?
status : I am using the mailer extension. It is working when I send to single address. But when I send to multiple addresses. It isn't working.
This one is working.
$mailer->AddAddress("aa@gmail.com");
followings are not working.
$mailer->AddAddress("aaa@gmail.com, bbbb@gmail.com");
$mailer->AddAddress("'aaa@gmail.com', 'bbbb@gmail.com'");
$mailer->AddAddress("\"aaa@gmail.com\", \"bbbb@gmail.com\"");
Lösung
You just have to call the "addAddress" function multiple times:
$mailer->AddAddress("aaa@gmail.com");
$mailer->AddAddress("bbbb@gmail.com");
Andere Tipps
Modify the Mailer class as following. Visit this thread for more information
<?php
Yii::import('application.extensions.PHPMailer_v5.1.*');
class Mailer {
private $mail;
public function initialise() {
try {
require Yii::getPathOfAlias('application.extensions') . '/PHPMailer_v5.1/class.phpmailer.php';
$this->mail = new PHPMailer(TRUE);
$this->mail->IsSMTP(); // tell the class to use SMTP
$this->mail->SMTPDebug = 0;
$this->mail->SMTPAuth = true; // enable SMTP authentication
$this->mail->Port = 25; // set the SMTP server port
$this->mail->Host = "smtp.test.net"; // SMTP server
$this->mail->Username = "test.com"; // SMTP server username
$this->mail->Password = "test"; // SMTP server password
$this->mail->Mailer = "smtp";
$this->mail->From = 'info@test.com';
$this->mail->FromName = 'test@net.com';
} catch (Exception $e) {
echo $e->getTraceAsString();
}
}
public function email($message, $sendTo, $subject) {
try {
$this->mail->AddAddress($sendTo);
$this->mail->Subject = $subject;
$body = $message;
$this->mail->MsgHTML($body);
$this->mail->IsHTML(true); // send as HTML
$this->mail->Send();
$this->mail->ClearAllRecipients();
} catch (Exception $e) {
echo $e->getTraceAsString();
}
}
}
?>
Simple way to understand single email...
$emailaddress="johndoe@domain.com"
$username="John Doe"
$mail->AddAddress($emailaddress,$username);
For multiple emails...
$mail->AddAddress("johndoe@domain.com");
$mail->AddAddress("johnsmith@domain.com");
Or you needs multiple emails in arrays...
foreach ($array as $value) {
$mail->AddAddress($array[$value]);
}
and in any loop condition that meet your requirement.
Additionally, you can create a message via Yii::app->mailer->newMessage.. This allows you to set email message values. For example:
$emailAddresses = array(
'to' => array('email@blah.com','email2@blah.com'),
'bcc' => array('multiple emails','separated','by','commas'),
'reply' => $replyEmail,
);
// Generate the message with appropriate fields
$message = Yii::app->mailer->newMessage; //Swift_Message::newInstance()
$message->setSubject($subject);
$message->setFrom(array($emailAddress => 'administration'));
$message->setTo( $emailAddresses['to'] );
$message->setBcc( $emailAddresses['bcc'] );
$message->setReplyTo( $emailAddresses['reply'] );
$message->setBody('<h1>'.$header.'</h1><p>'.$bodyHtml,'text/html');
//Send message
$mailer = Yii::app()->mailer->getInstance($email);
$mailer->send($message,$failures);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow