1

I have a problem and i'm really desperate. Here the situation:

I wanna send a newsletter html email via codeigniter. This is my controller:

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.";     

          }
       }


   }
}

P.S.:The phpmailer library has to be downloaded first. There is another function index in that script that loads the view.

This is my view where you can leave your mail adress so that you will receive the newsletter:

<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>

The html-email template consists of some interlaced tables. The template is displayed perfectly when opened within a browser. But when sent via mail it gots shred. The well formatted file is displayed incorrectly in the email client. The logo image and the special chars are displayed correctly but in the mail there is displayed a"< meta http-equiv="Content-Type" content="text/html; charset=utf-8">" as text (NOT html code) before the content starts. Some content is missing and some end tags ("") are doubled although in the original html file all is correct.

So where is the error? Is the file_get_contents()-Method not the right one to parse html documents? I already tried to use a different method with "file_get_html of the simple html dom (http://simplehtmldom.sourceforge.net/) but failed to run that library in codeigniter. I'm afraid i cant post the email html template because it's too long and the content contains sensitive information. But i dont think that the error is in the html file. Does anyone has a tip, some links or knows how this script could work?

P.S.: Sorry for my bad English!

Regards

user2718671
  • 2,866
  • 9
  • 49
  • 86

1 Answers1

0

Solved! Thanks to this link: http://www.jeremytunnell.com/posts/really-hairy-problem-with-seemingly-random-crlf-and-spaces-inserted-in-emails i found a solution for that bug:

using $mail->Encoding="base64"; fixed the problem

user2718671
  • 2,866
  • 9
  • 49
  • 86