PHP应用程序发送电子邮件HTML内容未显示

时间:2014-01-20 18:58:23

标签: php html-email

我有一个php应用程序,在用户注册帐户后发送电子邮件。该电子邮件由html组成。

有一天它停止了工作,我不知道为什么。它一直在发送电子邮件,但它没有内容。

我进入代码并注释掉了下面的方法,所以它只是发送原始html而不是以电子邮件可以读取的形式生成它,并且它有效。因此,我总结出问题在于以下方法。

    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);
    }

问题是,我不知道方法中的问题是什么,因为我认为在它停止工作之前我没有改变它,但逻辑表明我必须有。我不确定代码的其他区域是否会对它产生影响,但我认为这是一种相对自包含的方法。

我已经在$ html中设置的html下面发布,以防它重要。

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

有谁可以告诉我为什么没有正确发送电子邮件?

更新:我刚刚发现它只是将html通过电子邮件发送到我的Gmail帐户。当我尝试将其发送到非gmail帐户时,它也返回没有内容。

0 个答案:

没有答案