代码之家  ›  专栏  ›  技术社区  ›  unknown.person

如何发送多个settextbody yii2发送邮件?

  •  1
  • unknown.person  · 技术社区  · 7 年前

    yii/mail ,问题是我只能发送一个表单来成为文本体,我对模型进行了如下编码:

         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)
    

    因为在我看来 text input radio 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;}
    

    感谢Ankur Garg和Pratik Karmakar

    2 回复  |  直到 6 年前
        1
  •  3
  •   Ankur Garg    7 年前

    将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>
    
        2
  •  1
  •   Pratik Karmakar    7 年前

        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\u name”。