簡體   English   中英

在yii發送smtp郵件

[英]sending smtp mail in yii

我有一個在線休假申請表,我有一些字段,我可以創建,查看和修改該表格。 我還想再做一件事。 無論給出所有這些字段的值,例如休假類型,天數,原因等等。我只想獲取所有這些值,我想通過郵件發送它。在創建表單中,我將為所有字段賦值。當我點擊創建按鈕時,它應該發送一封包含這些細節的電子郵件

這是actionCreate的控制器代碼。 這里我懷疑這行=> $ mailmsg =(array('id'=> $ model-> leave_id));

public function actionCreate()
   {
    $model=new EmpLeave;
            $model->dateof_leave = date("Y-m-d H:i");
    if(isset($_POST['EmpLeave']))
    {
        $model->attributes=$_POST['EmpLeave'];
        if($model->save())
                        $recipients = "sridhar.venkatesan53@gmail.com";
                        $headers["From"] = "noreply@elixir.in";
                        $headers["To"] = "sridhar.venkatesan53@gmail.com";
                        $headers["Subject"] = "User feedback";
                        $mailmsg = (array('id'=>$model->leave_id));
                        /* SMTP server name, port, user/passwd */
                        $smtpinfo["host"] = "smtp.mandrillapp.com";
                        $smtpinfo["port"] = "587";
                        $smtpinfo["auth"] = true;
                        $smtpinfo["username"] = "noreply@elixir.in";
                        $smtpinfo["password"] = "oNkeBOEA5MfaN_24loUs1w";
                        /* Create the mail object using the Mail::factory method */
                        $mail_object =& Mail::factory("smtp", $smtpinfo);
                        /* Ok send mail */
                        $mail_object->send($recipients, $headers, $mailmsg);
            $this->redirect(array('view','id'=>$model->leave_id));
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

這是我的view.php

<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
    'leave_id',
    'leave_type',
    'leave_reason',
    'numof_days',
    'type',
            'dateof_leave',
   ),
 )); ?>

你忘記了{}

if($model->save()){
    .....
}

您想要將Inputed值作為Mail發送。 您的郵件正文看起來像一個數組。 它在發送之前作為字符串或html處理。

$mailmsg /*variable you are try to send as array .probably it must be string

$mailmsg=(array('id'=>$model->leave_id))  /*just try this for your own testing after you get idea about this*/
echo "<pre>";
print_r($mailmsg);
exit();

用於將帖子值從表單更改為消息文本的代碼

$mailmsg="<div><ul>";
    foreach($_POST as $key=>$value)
{
     $mailmsg.="<li>".$key." - ".$value."</li>";     // String Generated 
}
$mailmsg.="</ul></div>";

$ mailmsg包含html格式化字符串中表單輸入的數據

  public function actionCreate()
   {
    $model=new EmpLeave;
            $model->dateof_leave = date("Y-m-d H:i");
    if(isset($_POST['EmpLeave']))
    {
        $model->attributes=$_POST['EmpLeave'];
        if($model->save())
{
                        require_once(Yii::app()->basePath.'/extensions/PearMail/Mail-1.2.0/Mail.php');


                        $recipients = "sridhar.venkatesan53@gmail.com";
                        $headers["From"] = "noreply@elixir.in";
                        $headers["To"] = "sridhar.venkatesan53@gmail.com";
                        $headers["Subject"] = "User feedback";
                        $mailmsg="<div><ul>";
                        foreach($_POST as $key=>$value)
                        {
                             $mailmsg.="<li>".$key." - ".$value."</li>";     
                        }
                        $mailmsg.="</ul></div>";
                        /* SMTP server name, port, user/passwd */
                        $smtpinfo["host"] = "smtp.mandrillapp.com";
                        $smtpinfo["port"] = "587";
                        $smtpinfo["auth"] = true;
                        $smtpinfo["username"] = "noreply@elixir.in";
                        $smtpinfo["password"] = "oNkeBOEA5MfaN_24loUs1w";
                        /* Create the mail object using the Mail::factory method */
                        $mail_object =& Mail::factory("smtp", $smtpinfo);
                        /* Ok send mail */
                        $mail_object->send($recipients, $headers, $mailmsg);
            $this->redirect(array('view','id'=>$model->leave_id));
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

這段代碼足以發送。 你只需要更換你的細節和身體信息

Yii::import('application.extensions.phpmailer.JPhpMailer');
$mail = new JPhpMailer;
$mail->IsSMTP();
$mail->Host = 'smpt.163.com';     // your host and smtp address
$mail->SMTPAuth = true;
$mail->Username = 'yourname@163.com';      //login username
$mail->Password = 'yourpassword';          //login password
$mail->SetFrom('yourname@163.com', 'yourname');   //from 
$mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress('john.doe@otherdomain.com', 'John Doe');
$status=$mail->Send();
print_r($status);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM