当我使用 codeigniter 电子邮件类发送电子邮件时显示 html 源的电子邮件
Posted
技术标签:
【中文标题】当我使用 codeigniter 电子邮件类发送电子邮件时显示 html 源的电子邮件【英文标题】:Email showing html source when i send email using codignator email class 【发布时间】:2012-12-29 04:49:22 【问题描述】:我在使用 codeignitor 电子邮件类在 codeignitor 中发送带有附件的 html 邮件时遇到问题,邮件显示 html 代码而不是 html 视图。
我在下面的配置中将邮件类型设置为 html 是我的代码
$message="<p>test</p>";
$mail_to = "email@gmail.com";
$from_mail = $useremail;
$from_name = $userfname;
$reply_to = $useremail;
$subject = "Abstract Details";
$file_name = $datamail['varafile'];
$path = realpath('uploads/abstract');
// Read the file content
$file = $path.'/'.$file_name;
$config = array (
'protocol' =>'sendmail',
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from($from_mail,$from_name);
$this->email->to($mail_to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach($file);
if($this->email->send())
echo "Mail send successfully";
else
echo "Error in sending mail";
【问题讨论】:
@Adam West*** 回答你的问题了吗?因为你应该承认它。或者,如果你想通了,你应该发布你的解决方案。只是说,这就是它的工作原理。 【参考方案1】:这样配置
$config = Array(
'protocol' => 'sendmail',
'mailtype' => 'html',
'smtp_host' => '', //your SMTP host
'smtp_port' => 26,
'smtp_user' => '', //your SMTP email address
'smtp_pass' => '', //your SMTP email password
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_header('MIME-Version', '1.0; charset=utf-8'); //must add this line
$this->email->set_header('Content-type', 'text/html'); //must add this line
$this->email->from('', ''); //from/sender email [with name (optional)]
$this->email->to(); //to/receiver email
$this->email->subject(''); //email subject
$message = $this->load->view('...directory.../...filename...',$data,TRUE); //html template/body with dynamic data
$this->email->message($message);
$this->email->send();
【讨论】:
$this->email->set_header('MIME-Version', '1.0; charset=utf-8'); $this->email->set_header('Content-type', 'text/html');以上两行对我有用:)【参考方案2】:[向所有人道歉,我没有足够的代表来评论澄清]
我刚刚尝试了您的确切代码(减去附件位)并且一切正常,将 HTML 电子邮件发送到 Gmail 帐户,我将在下面提供我的完整工作类:
class rohithipix extends CI_Controller
public function html_email()
$message = "<h1>start</h1><p>test</p>";
$mail_to = "test@gmail.com";
$from_mail = 'test2@gmail.com';
$from_name = 'dave';
$reply_to = 'test2@gmail.com';
$subject = "Abstract Details";
//$file_name = $datamail['varafile'];
//$path = realpath('uploads/abstract');
// Read the file content
//$file = $path . '/' . $file_name;
$config = array(
'protocol' => 'sendmail',
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($from_mail, $from_name);
$this->email->to($mail_to);
$this->email->subject($subject);
$this->email->message($message);
//$this->email->attach($file);
if ($this->email->send())
echo "Mail send successfully";
else
echo "Error in sending mail";
您在测试时尝试将这些发送到哪个电子邮件客户端?
【讨论】:
以上是关于当我使用 codeigniter 电子邮件类发送电子邮件时显示 html 源的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
使用 codeigniter 通过 gmail smtp 服务器发送电子邮件
发送 HTML 电子邮件导致电子邮件显示 HTML 源代码(Codeigniter 电子邮件类)