Codeigniter 电子邮件库发送空白电子邮件或 html 源代码
Codeigniter email library sending blank emails or html source code
我之前通过 CodeIgniter 发送电子邮件从来没有遇到过问题,但突然之间我的最新项目出现了问题。
我正在使用这个 mail server tool 在我的 local-host 上发送邮件,它以前从未给我任何问题,然后是 CodeIgniter 电子邮件库。
我可以得到 2 个结果之一: 电子邮件已发送,但显示所有原始 HTML 源代码,或者电子邮件已发送并有主题行,但整个 body 是空白的。
这是我的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');
希望对您有所帮助:
您在加载视图部分缺少 $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
我之前通过 CodeIgniter 发送电子邮件从来没有遇到过问题,但突然之间我的最新项目出现了问题。
我正在使用这个 mail server tool 在我的 local-host 上发送邮件,它以前从未给我任何问题,然后是 CodeIgniter 电子邮件库。
我可以得到 2 个结果之一: 电子邮件已发送,但显示所有原始 HTML 源代码,或者电子邮件已发送并有主题行,但整个 body 是空白的。
这是我的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');
希望对您有所帮助:
您在加载视图部分缺少 $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