2017-09-29 27 views
0

こんにちは、ここでyiimailを使用しています。私は私のコードはYiiは複数の受信者に電子メールを送信します

$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あり、ここで複数の受信者に

メールを送信したいデータベースからユーザーと$model->receiver1からの入力値を取得します。ユーザーが$mailCCの2人の他のユーザーを入力した場合、これは最初のメールにのみ送信され、両方には送信されません。

例:

$model->[email protected] 
$mailCC = array("[email protected]", "[email protected]") //this is from user input 

メールはiのみが

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

を試みたが、それは

preg_match() expects parameter 2 to be string, array given

このエラーを返す [email protected] & [email protected]

に送信します

どこが間違っていましたか?

答えて

0

は、私はこの問題は をやってしまっているここに私のコードでは、我々は、セパレーターを爆発ループ受信者およびuの連中に

おかげsetToないADDTOに変更する必要があります

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

ですいくつかの解決策があります

0

あなたはうまくいけば、それが動作します。この

$mail->addCC($mailCC[0]); 
$mail->addCC($mailCC[1]); 

を試すことができます。ここに私の完全な実行コード

$message = new YiiMailMessage; 
    $message->view = 'registrationFollowup'; 

    //userModel is passed to the view 
    $message->setBody(['userModel' => "test"], 'text/html'); 

    $message->setTo("[email protected]"); 
    $message->addCC('[email protected]'); 
    $message->addCC('[email protected]'); 
    $message->from = "[email protected]"; 
    $status = Yii::app()->mail->send($message); 
    print_r($status); //print the number of recipient, which 3 
+0

このコードを試すときにこのエラーが発生しました。「無効なオフセットタイプ」 – Trainee

+0

@ Trainee今すぐお試しください。私は実行中のスニペットを追加しました –

+0

遅い返信のためにsryを追加しました。それは動的なデータにならないようにしていれば、ユーザは2 CC以上、2以上の値を送信することはできませんとにかく – Trainee

関連する問題