0

I have a php application which sends an email out after a user registers an account. The email is made up of html.

One day it stopped working, and I'm not sure why. It kept sending emails, but it had no content.

I went into the code and commented out the below method, so it just sent the raw html instead of generating it in a form the email could read, and it worked. I therefore concluded the problem was in the method below.

    private function generateEmailHTML($html)       // Convert the HTML to email friendly html
    {
        preg_match_all('~<img.*?src=.([\/.a-z0-9:_-]+).*?>~si', $html, $matches);

        $i = 0;

        $paths = array();

        foreach ($matches[1] as $img) 
        {
            $img_old = $img;

            if(strpos($img, "http://") == false) 
            {
              $uri = parse_url($img);

              $paths[$i]['path'] = $_SERVER['DOCUMENT_ROOT'].$uri['path'];

              $content_id = md5($img);

              $html = str_replace($img_old,'cid:'.$content_id,$html);

              $paths[$i++]['cid'] = $content_id;
            }
        }

        preg_match_all('~<style.*?url.([\/.a-z0-9:_-]+).*?>~si',$html,$matches);

        foreach ($matches[1] as $img) 
        {
            $img_old = $img;

            if(strpos($img, "http://") == false) 
            {
                $uri = parse_url($img);

                $paths[$i]['path'] = $_SERVER['DOCUMENT_ROOT'].$uri['path'];

                $content_id = md5($img);

                $html = str_replace($img_old,'cid:'.$content_id,$html);

                $paths[$i++]['cid'] = $content_id;
            }
        }

        $boundary = "--".md5(uniqid(time()));       

        $this->headers  = "From: Agents <".$this->from."> \n";

        $this->headers .= "Reply-To: ".$this->from." \n";

        $this->headers .= "BCC: testing@email.co.uk, info@email.com \n";

        $this->headers .= "MIME-Version: 1.0\n";

        $this->headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\" \n";

        $multipart = '';

        $multipart .= "--$boundary\n";

        $kod = 'utf-8';

        $multipart .= "Content-Type: text/html; charset=$kod\n";

        $multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n";

        $multipart .= "$html\n\n";

        $usedImages = array();

        foreach ($paths as $path) 
        {
            if(file_exists($path['path']))
            {
                $fp = fopen($path['path'],"r");
            }

            if(!$fp)
            {
                return false;
            }

            $imageLoaded = false;

            for($i = 0; $i < sizeof($usedImages); $i++) 
            {
                if($path['path'] == $usedImages[$i])
                {
                    $imageLoaded = true;
                }
            }

            if(!$imageLoaded) 
            {
                $imagetype = substr(strrchr($path['path'], '.' ),1);

                $file = fread($fp, filesize($path['path']));

                fclose($fp);

                $message_part = "";

                switch ($imagetype) 
                {
                  case 'png':

                  case 'PNG':

                        $message_part .= "Content-Type: image/png";

                        break;

                  case 'jpg':

                  case 'jpeg':

                  case 'JPG':

                  case 'JPEG':

                        $message_part .= "Content-Type: image/jpeg";

                        break;

                  case 'gif':

                  case 'GIF':

                        $message_part .= "Content-Type: image/gif";

                        break;
                }

                $message_part .= "; file_name = \"$path\"\n";

                $message_part .= 'Content-ID: <'.$path['cid'].">\n";

                $message_part .= "Content-Transfer-Encoding: base64\n";

                $message_part .= "Content-Disposition: inline; filename = \"".basename($path['path'])."\"\n\n";

                $message_part .= chunk_split(base64_encode($file))."\n";

                $multipart .= "--$boundary\n".$message_part."\n";

                array_push($usedImages, $path['path']);

            }

        }

        $multipart .= "--$boundary--\n";

        return array('multipart' => $multipart, 'headers' => $headers);
    }

Problem is, I have no idea what the problem in the method is, as I don't think I've changed it before it stopped working, but logic suggests I must have. I'm not sure if other areas of the code might have an influence on it, but I thought it was a relatively self contained method.

I've posted below the html that is set in $html in case it is important.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <style type="text/css">

        body {

            font-family: Arial;

            font-size: 15px;

            color: #222222;

        }

        a {

            color: #1155CC;

        }

        span {

            color: #FEC110;

        }

    </style>

</head>

<body>
    text text text... 

    <a href="http://www.website.com" target="_blank" style="color: #000000; text-decoration: none;">www.website.com</a>

    <br/>

    <a href="mailto: support@email.com" style="color: #000000; text-decoration: none;">support@email.com</a>

    <br/>

    <img src="http://www.website.co.uk/image.png" alt="" />


    <br/><br/>

</body>

</html>

Can anyone tell me why it isn't emailing properly?

UPDATE: I've just discovered it is only emailing out the html to my gmail account. When I try to send it to a non gmail account, it also returns with no content.

Pippa Rose Smith
  • 1,377
  • 5
  • 18
  • 42
  • For your reference http://stackoverflow.com/questions/3902455/smtp-multipart-alternative-vs-multipart-mixed – Saurabh Sharma Jan 20 '14 at 19:01
  • Is it at all possible for you to access the mail/smtp logs on the server? If yes please take a look at them (around the period the behaviour of your mailer changed) and update us with the erros if there is any. – ghousseyn Jan 20 '14 at 19:27
  • I'm not sure, I'll take a look and see if I can find them – Pippa Rose Smith Jan 21 '14 at 09:57

0 Answers0