我以前从未通过CodeIgniter发送电子邮件,但是突然之间,这是我最新项目中的一个问题。
我正在使用此mail server tool在本地主机(以前从未给过我任何问题)上发送邮件,然后在CodeIgniter电子邮件库上发送邮件。
我可以得到2个结果之一:要么发送电子邮件,但显示所有原始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' => '\r\n',
'crlf' => '\r\n'
);
//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');
答案 0 :(得分:1)
希望这对您有帮助:
您在加载视图部分缺少了$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' => '\r\n',
'crlf' => '\r\n'
);
$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