首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在codeigniter中正确使用phpmailer和html-email-template?

如何在codeigniter中正确使用phpmailer和html-email-template?
EN

Stack Overflow用户
提问于 2013-08-27 02:37:06
回答 1查看 3.4K关注 0票数 1

我有个问题,我真的很绝望。情况是这样的:

我想通过codeigniter发送一封时事通讯html电子邮件。这是我的控制器:

代码语言:javascript
运行
复制
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库必须先下载。该脚本中还有另一个函数索引,用于加载视图。

这是我的观点,你可以在这里留下你的邮件地址,这样你就会收到时事通讯:

代码语言:javascript
运行
复制
<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">&nbsp;</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文件中。有谁有提示,一些链接或知道这个脚本是如何工作的吗?“

附言:对不起,我的英语不好!

问候

EN

回答 1

Stack Overflow用户

发布于 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";解决了这个问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18450731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档