繁体   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