简体   繁体   English

在codeigniter中发送电子邮件时如何将html页面作为消息发送?

[英]How to send html page as message when sending email in codeigniter?

I want to send a html page as message when sending email in codeigniter..在codeigniter中发送电子邮件时,我想发送一个html页面作为消息..

$this->email->set_mailtype("html");
$this->email->from('xyz@gmail.com', 'ABC');
$this->email->to($this->input->post('emailid'));            
$this->email->subject('New Subject');   

$message = //HERE I WANT TO INCLUDE A FILE TO END AS MESSAGE    

$this->email->message($message);        
$this->email->send();

Could it be possible ??可能吗?? Please help...请帮忙...

You can simply use the method file_get_contents()您可以简单地使用方法file_get_contents()

$message = file_get_contents("/path/to/htmlfile");

Also you can use the codeigniter way您也可以使用codeigniter 方式

$template = $this->load->view(APPATH.'email/file', $your_data, true);

What I have been using in my project, I created a function in a common_helper named as sendMail我在我的项目中一直使用的,我在一个名为sendMail的 common_helper 中创建了一个函数

function sendMail($subject, $mailContent, $mailTo, $mailFromId, $mailFromName)
{
    $CI =& get_instance();
    $CI->load->library('email');
    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $CI->email->clear(TRUE);
    $CI->email->initialize($config);
    $CI->email->from($mailFromId, $mailFromName);
    $CI->email->to($mailTo);
    $CI->email->subject($subject);
    $CI->email->message($mailContent);
    $CI->email->send();
}

You can call this function anywhere from your controller to send mail.您可以从控制器的任何位置调用此函数来发送邮件。 Regarding HTML content of email should be loaded as Agam Banga mentioned above:关于电子邮件的 HTML 内容应加载为上述 Agam Banga:

$mailContent = $this->load->view('email/template', $data, true);

This varibale can be passed simply calling like这个变量可以简单地通过调用像

sendMail($subject, $mailContent, $mailTo, $mailFromId, $mailFromName);

In your controller.在您的控制器中。

Let me know if you face any issue.如果您遇到任何问题,请告诉我。

Here the code of Model and sending mail Using Email Library.这里是模型和使用电子邮件库发送邮件的代码。 And auto load library use it on your config folder -> autoload file $autoload['libraries'] = array('email');并且自动加载库在您的配置文件夹中使用它 -> 自动加载文件 $autoload['libraries'] = array('email');

public function mail_send($mdata){公共函数mail_send($mdata){

          $this->load->library('email');
          $this->email->set_mailtype('html');
         //$this->email->set_newline("\r\n");
          $this->email->from($mdata['email']); // change it to yours
          $this->email->to('xyz@gmail.com');// change it to yours
          $this->email->cc($mdata['email']);
          $this->email->subject('Hello');
          //$this->email->message($mdata['address']);

          $body= $this->load->view('pages/mail', $mdata, true);
          $this->email->message($body);
          // echo '<pre>';
          // print_r($mdata);
          // exit();

          $this->email->send();


          $this->email->clear();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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