2017-09-27 111 views
0

嗨我試圖做一個表格,可以發送電子郵件使用yii/mail,問題是我只能發送一種形式成爲文本正文,我已經編碼這樣的模型如何發送多個settextbody yii2發送郵件

 class CareerForm extends Model 

    { 

     public $name; 

     public $email; 

     public $subject; 

     public $body; 

     public $verifyCode; 

     public $gender; 

     public function rules() 

     { 

      return [ 

      [['name', 'email', 'subject', 'body','gender'], 'required'], 

      ['email', 'email'], 

      ['verifyCode', 'captcha'], 

      ]; 

     } 
     public function career($email) 

     { 

      if ($this->validate()) { 

      Yii::$app->mailer->compose() 

       ->setTo($email) 

       ->setFrom([$this->email => $this->name]) 

       ->setSubject($this->subject) 

       ->setTextBody($this->body) 

       ->send(); 



      return true; 

     } 

     return false; 

     } 
    } 

如何使用多個參數來

->setTextBody($this->body) 

->setTextBody($this->body,$this->gender) 

BEC在我看來,我有幾個text inputradio list作爲電子郵件發送,我該怎麼做?

我的短信預期將是這樣的:

name 
gender 
variable 1 
variable 2 
variable n 

摘要 編輯=兩者的答案是正確的,但我用

public function career($email) 
{ 
    if ($this->validate()) { 
     Yii::$app->mailer->compose('filename.html' ,[ 
      'email' => $this->email, 
      'name' => $this->name, 
      ]) 
      ->setTo($email) 
      ->setFrom([$this->email => $this->name]) 
      ->setSubject('career') 
      ->send(); 

     return true; 
    } 
    return false;} 

感謝安高爾·加和PRATIK Karmakar

+0

準備電子郵件的身體befroe調用 - > setTextBody($ this-> body),然後設置爲$ this-> body –

+0

如果您說先準備好電子郵件正文,那麼如何將2個變量(body和gender)合併爲一個變量(body)? –

+1

正文是一個字符串。試試這個 $ this-> body。= $ this-> gender; –

回答

2

很好的做法,把你的HTML單獨的文件,並在該文件中

$body = $this->renderPartial('_mail_body.php' .[ 
       'gender' => $this->gender, 
       'name' => $this->name, 
    ]); 

準備郵件正文和內容_mail_body.php會是這樣

<html> 
<body> 
<table cellpadding="0" cellspacing="0" align="center" width="672px" style="font-size:24px; text-align:center;"> 
<tr> 
    <td width="670px" align="center" style="border-left:1px solid #e0e0e0; border-right:1px solid #e0e0e0; font-size:24px; font-family:Arial, Helvetica, sans-serif; padding-top:47px;"> 
     <table width="608px" cellpadding="0" cellspacing="0" align="center"> 
      <tr> 
       <td width="178px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; border-right:1px solid #e0e0e0; padding:11px;"> 
        Name 
       </td> 
       <td width="427px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px;padding:11px; color:#4e4e4e;"> 
        <?php echo $name;?> 
       </td> 
      </tr> 
      <tr> 
       <td width="178px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; border-right:1px solid #e0e0e0; padding:11px;"> 
        Gender 
       </td> 
       <td width="427px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px;padding:11px; color:#4e4e4e;"> 
        <?php echo $gender;?> 
       </td> 
      </tr> 
     </table> 
    </td> 
</tr> 
</table> 
</body> 
</html> 
+0

感謝您的答案,但我沒有使用renderpartial,而是我用 - > compose(「file_name」,['body'=> $ this-> body,'gender'=> $ this-> gender] ,...),謝謝你幫我 –

+0

沒問題。隨時歡迎 !! –

1

傳遞多個參數的最佳方式是在郵件目錄內創建另一個視圖文件並傳遞參數。

public function career($email) 
    { 
     $message = Yii::$app->mailer->compose("file_name", ['body' => $this->body,'gender'=>$this->gender]) 
        ->setTo($email) 
        ->setFrom([$this->email => $this->name]) 
        ->setSubject($this->subject) 
        ->setTextBody($this->body) 
        ->send(); 

    } 

注:創建郵件目錄中的文件 'FILE_NAME'。

+0

)?或者參數應該用「file_name」更改? –

+0

不,您可以刪除選項setTextBody,因爲現在不需要它。像在渲染文件 –

+1

時所做的那樣,將所有參數傳遞給「file_name」,謝謝,這也是正確答案,但其他答案給出了「file_name」示例,但仍然是正確答案,所以我投票贊成,謝謝 –