我有个问题,我真的很绝望。情况是这样的:
我想通过codeigniter发送一封时事通讯html电子邮件。这是我的控制器:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class mail extends CI_Controller {
function __construct(){
parent::__construct();
}
function send_email() {
$this->load->view('mail_form');
$this->load->library('phpmailer');
$this->load->helper('url');
if(isset($_POST['btnsend'])) {
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->CharSet="UTF-8";
$path=base_url() . 'assets/mail.html';
$body=file_get_contents($path);
$mail->SetFrom('bla@bla.com','blabla.com');
address = $_POST['email'];
$mail->AddAddress($address, "Guest");
$mail->Subject = "bla";
$mail->MsgHTML($body);
$mail->AddEmbeddedImage('logo.jpg', 'logo', 'logo.jpg','base64','image/jpeg');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "A test email sent to your email address '".$_POST['email']."' Please Check Email and Spam too.";
}
}
}
}
附言: phpmailer库必须先下载。该脚本中还有另一个函数索引,用于加载视图。
这是我的观点,你可以在这里留下你的邮件地址,这样你就会收到时事通讯:
<html>
<head>
<title>Send Email</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="<?php echo base_url('index.php/mail/send_email'); ?>">
<table width="461" height="138" border="1">
<tr>
<td width="141">Enter Your Email Here</td>
<td width="187">
<input name="email" id="email" type="text" />
<input type="submit" name="btnsend" id="btnsend" value="Send Email" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</body>
</html>
html-email模板由一些交错的表格组成。在浏览器中打开模板时,可以完美地显示该模板。但是当通过邮件发送时,它会被撕碎。格式正确的文件在电子邮件客户端中显示不正确。徽标图像和特殊字符显示正确,但在邮件中,在内容开始之前会显示“< meta http-equiv=" content -Type”content=" text / html;charset=utf-8">“文本(而不是html代码)。一些内容丢失,一些结束标记("")被重复,尽管在原始html文件中都是正确的。
那么错误在哪里呢?file_get_contents()-Method不是解析html文档的正确方法吗?我已经尝试使用不同的方法"file_get_html of the simple html dom (http://simplehtmldom.sourceforge.net/),但未能在codeigniter中运行该库。恐怕我无法发布电子邮件html模板,因为它太长,内容包含敏感信息。但我不认为错误出在html文件中。有谁有提示,一些链接或知道这个脚本是如何工作的吗?“
附言:对不起,我的英语不好!
问候
发布于 2013-08-27 15:59:05
解决了!多亏了这个链接:http://www.jeremytunnell.com/posts/really-hairy-problem-with-seemingly-random-crlf-and-spaces-inserted-in-emails,我找到了这个bug的解决方案:
使用$mail->Encoding="base64";
解决了这个问题
https://stackoverflow.com/questions/18450731
复制相似问题