2015-06-11 29 views
1

我使用codeigniter发送带有php电子邮件功能的电子邮件。如何在php中添加变量读取文件

对于我已经创建的模板文件,并使用它具有读取文件功能,

$message = $this->input->post('reply_content'); 
$this->load->helper('file'); 
$newbody = read_file(base_url().'/uploads/template.php'); 

//电子邮件发送类似下面

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

    $this->email->from('[email protected]', 'X'); 
    $this->email->to($this->input->post('reply_to')); 

    $this->email->subject($subject); 
    $this->email->message($newbody);  
    $this->email->cc($this->input->post('reply_cc')); 
    $this->email->send(); 

的template.php是我的模板文件,该文件是HTML格式。 我需要的是在template.php中有$ message,所以当用户发送电子邮件时,电子邮件中包含一个变量值,

如何在模板中添加变量,然后读取并发送为电子邮件?

回答

0

你不想加载库file我相信你知道如何将数据放入视图。让我们试试这个

$data['message']=$this->input->post('reply_content'); 

$newbody=$this->load->view('/uploads/template',$data,true); 
+0

我不想看到它,但我需要阅读文件存储为字符串,但该字符串一个变量值应插入和然后发送为电子邮件正文。 – rjcode

+0

从我的答案中删除** echo **现在您可以获取模板作为变量$ newbody –

+0

中的字符串,您可以将任何数据传递到您的页面模板,这就是为什么我把它作为视图。这将是您的约束的有效解决方案 –

1

你需要使用如下解析:

$from = $this->config->item('from_email_id'); 
$to = $this->input->post('userEmail'); 
$subject = 'Welcome'; 
$email_data= array(
     'first_name' =>$user_info->first_name, 
     'last_name' =>$user_info->last_name, 
     'numeric_code' => $user_info->numeric_code 
     ); 
$msg_body = $this->parser->parse('templates/activate_account', $email_data ,true); 
+0

谢谢,我刚刚阅读解析器帮手,这工作正常。 – rjcode