如何使用codeigniter中的View发送包含内容的电子邮件

时间:2012-05-19 07:33:58

标签: codeigniter php

我想从我的应用程序向用户发送一封电子邮件,其中包含从视图加载的电子邮件的内容。这是我到目前为止尝试过的代码:

$toemail = "user@email.id";

$subject = "Mail Subject is here";
$mesg = $this->load->view('template/email');

$this->load->library('email');

$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';

$this->email->initialize($config);

$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();

4 个答案:

答案 0 :(得分:17)

  1. 需要在控制器中调用$this->load->library('email');以及CI中的电子邮件才能正常工作。
  2. 此外,在您的代码中:$fromemail未初始化。
  3. 您的服务器上需要 SMTP支持
  4. $ config应在分配值和键之前声明为数组
  5. 工作代码:

    $this->load->library('email');
    $fromemail="ad@c.com";
    $toemail = "user@email.id";
    $subject = "Mail Subject is here";
    $data=array();
    // $mesg = $this->load->view('template/email',$data,true);
    // or
    $mesg = $this->load->view('template/email','',true);
    
    
    $config=array(
    'charset'=>'utf-8',
    'wordwrap'=> TRUE,
    'mailtype' => 'html'
    );
    
    $this->email->initialize($config);
    
    $this->email->to($toemail);
    $this->email->from($fromemail, "Title");
    $this->email->subject($subject);
    $this->email->message($mesg);
    $mail = $this->email->send();
    

    修改     $mesg = $this->load->view('template/email',true);应该是由lycanian指出的 true 。通过将其设置为true,它不会将数据发送到输出流,但会以字符串形式返回。

    修改 $this->load->view();需要第二个带数据的参数,或者像$mesg = $this->load->view(view,data,true);一样空,如果没有,它将不起作用

答案 1 :(得分:8)

此行 $ mesg = $ this-> loading->视图('模板/电子邮件',true); 应如下所示 $ mesg = $ this-> load-> view(' template / email','',true);
在值为true之前使用单引号,它将完美地工作

答案 2 :(得分:2)

电子邮件模板发送在codeigniter中,我们需要在发送电子邮件之前放置和元标记

$this->data['data'] = $data;
$message = $this->load->view('emailer/create-account', $this->data,  TRUE);
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from($email, $name);
$this->email->to('emailaddres@mail.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();

答案 3 :(得分:-1)

你会试试!!

面临许多错误后,它正在为我的工作
public static void main(String[] args) {
        Collection<Integer> l = new CopyOnWriteArrayList<Integer>();

        for (int i=0; i < 10; ++i) {
            l.add(new Integer(4));
            l.add(new Integer(5));
            l.add(new Integer(6));
        }

        for (Integer i : l) {
            if (i.intValue() == 5) {
                l.remove(i);
            }
        }

        System.out.println(l);
    }