简体   繁体   中英

I'm trying to create and html page as a body for an email. How do I pass it into my setBody() using yiiMail?

I have a .php page with the following code:

 <html>
<head></head>
<body>
    Hi <?php echo $user['first_name'].' '.$user['last_name']?>,
    <br><br>
    To reset your password, please go to the following page:
    <br>
    <?php 
            $url = Yii::app()->createAbsoluteUrl('user/reset/id/'.  $key);
            echo "<a href='{$url}'>{$url}</a>";
    ?>
    <br>
    Your password will be automatically reset, and a new password will be emailed to you.        
</body>

I want to be able to put this inside a function I created in the Controller inside setBody() here:

  public function emailAll($url)
    {

        $this->set_mail_settings();
        $message = new YiiMailMessage;        

            $emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons WHERE party_id != 184")->queryRow();
            $email_ids = explode(",",$emails["em"]);
            $message->setBcc($email_ids);
            $message->setBody('To view, click here: '.$url);
            $message->subject = 'New Announcement Posted!';
            $message->addTo('dcp@gmail.com');
            $message->from = Yii::app()->params['adminEmail'];
            Yii::app()->mail->send($message);                      
    }

The URL link that I want inside the body is coded from the actionCreate() which is here:

 $currentid = Yii::app()->db->createCommand("select id from content where id = (select max(id) from content)")->queryRow();
                Yii::app()->session['announcement_message'] = 'You have successfully created an announcement.';

                $url = Yii::app()->createAbsoluteUrl('announcement/view/id/'.  $currentid["id"]);

                $this->emailAll($url);

                $this->redirect(array('view','id'=>$model->id));

I'm just now trying to figure out how I would send that .php page that has html inside into the setBody() along with $url I passed. Any help?

To do this, you store the mail template .php file as a view in your corresponding view folder, then call

$message->setBody(
    $this->renderPartial("_yourTemplateFileName",
             array("key"=>$key,'user'=>$user),true,false)
 );

RenderPartial can process a file and return the output by setting the third parameter return as true, the fourth parameter is for processing Js stuff not required in this context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM