我以前从未遇到过通过CodeIgniter发送电子邮件的问题,但突然间,这是我最新项目的一个问题。
我使用这个邮件服务器工具在我的本地主机上发送邮件,以前从未给我任何问题,然后是CodeIgniter电子邮件库。
我可以得到以下两个结果之一:要么电子邮件发送,但显示所有原始HTML源代码,要么电子邮件发送并有主题行,但整个正文为空。
这是我的email_helper.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
function send_email($to, $subject, $template)
{
$ci = &get_instance();
$ci->load->library('email');
$config = array(
'mailtype' => 'html',
'newline' => 'rn',
'crlf' => 'rn'
);
//If I comment out this line, it sends raw HTML, otherwise it sends a blank body.
$ci->email->initialize($config);
$ci->email->from($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
$ci->email->reply_to($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
$ci->email->to($to);
$ci->email->subject($subject);
$ci->email->message($ci->load->view('email/' . $template . '_html', $data, TRUE));
$ci->email->send();
}
这是我的test_html.php
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<div style="max-width: 800px; margin: 0; padding: 30px 0;">
TEST!
</div>
</body>
</html>
然后我从我的控制器打电话给电子邮件助手,上面写着:
$this->load->helper('email_helper');
send_email($this->input->post('email'), 'Test Subject', 'test');
希望这将帮助您:
您在加载视图部分缺少$data
,还尝试使用$ci->load->library('email', $config);
而不是$ci->email->initialize($config);
send_email
应该是这样的:
function send_email($to, $subject, $template)
{
$ci = & get_instance();
$config = array(
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'newline' => 'rn',
'crlf' => 'rn'
);
$data = '';
$body = $ci->load->view('email/' . $template . '_html', $data, TRUE);
echo $body; die;
$ci->load->library('email', $config);
$ci->email->set_mailtype("html");
$ci->email->from($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
$ci->email->reply_to($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
if ($to)
{
$ci->email->to($to);
$ci->email->subject($subject);
$ci->email->message($body);
if ($ci->email->send())
{
return TRUE;
}
else
{
echo $ci->email->print_debugger();die;
}
}
}
更多信息:https://www.codeigniter.com/user_guide/libraries/email.html