如何修复在电子邮件中发送附件

时间:2017-10-06 09:30:05

标签: php email

我的电子邮件服务附件存在问题。

我有一个电子邮件服务(简单)..我的服务仅用于接收电子邮件。所以..当我向我的服务发送电子邮件时,我会收到电子邮件,但如果附带附件,附件会显示文件名(例如image.jpg / file1.zip / file2.rar)..但链接不起作用..我不能下载等..当我在我的电子邮件的文本中包含图像时,我看不到图像,但如果我转发电子邮件并发送到我的服务,我已经收到在该领域的图像。

检查我的代码..

<?php
$id = $_POST['id'];
$pdo = new PDO("sqlite:db.db");
$email = $pdo->query("SELECT * FROM `testemail` WHERE `id`='$id'")->fetch(PDO::FETCH_ASSOC);

$email_body = base64_decode($email['message']);

$letter_html = "";
//$op_getKey = preg_match_all("/Content-Type: multipart\/alternative; boundary=\"(.*?)\"/", $email_body, $getKey);
//$op_getKey = preg_match_all("/alternative; boundary=\"(.*)\"/", $email_body, $getKey);
$op_getKey = preg_match_all("/boundary=\"(.*)\"/", $email_body, $getKey);
//$has_attachment = preg_match_all("/multipart\/mixed/; boundary=\"(.*?)\"/", $email_body, $attachment);
$has_attachment = preg_match_all("/multipart\/mixed/", $email_body, $attachment);
if ($op_getKey) {
    if ($has_attachment) {
        $mailParts = explode("--" . $getKey[1][1], $email_body);
        $attachParts = explode("--" . $getKey[1][0], $email_body);

        $get_attach_name=preg_match_all("/filename=\"(.*?)\"/", $attachParts[2], $attach_name);
        $get_attach_type=preg_match_all("/Content-Type: (.*?);/", $attachParts[2], $attach_type);
        $pure_code = explode("\n\n",$attachParts[2]);
        $pure_code = str_replace("\r","",$pure_code[1]);
        $pure_code = str_replace("\n","",$pure_code);
    }else{
        $mailParts = explode("--" . $getKey[1][0], $email_body);
    }
        //$letter_text = str_replace('Content-Type: text/plain; charset="UTF-8"', '', $mailParts[1]);
        //echo $letter_text;

    $the_mail = $mailParts[2];


    $to_replace = array(
        'Content-Type: text/html; charset="UTF-8"',
        'Content-Type: text/html; charset=UTF-8',
        'Content-Type: text/html; charset="utf-8"',
        'Content-Type: text/html; charset=utf-8',
        'Content-Type: text/html; charset="iso-8859-1"',
        'Content-Type: text/html; charset=iso-8859-1',
        'Content-Type: text/html; charset="ISO-8859-1"',
        'Content-Type: text/html; charset=ISO-8859-1',
        'Content-Type: text/plain; charset="iso-8859-1"',
    );

    foreach ($to_replace as $k => $v) {
        $the_mail = str_replace($to_replace[$k], '', $the_mail);
    }

      //$the_mail = str_replace('Content-Type: text/html; charset="UTF-8"', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset=UTF-8', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset="utf-8"', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset=utf-8', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset="iso-8859-1"', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset=iso-8859-1', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset="ISO-8859-1"', '', $the_mail);
      //$the_mail = str_replace('Content-Type: text/html; charset=ISO-8859-1', '', $the_mail);


    if (preg_match_all("/Content-Transfer-Encoding: quoted-printable/", $the_mail, $mailToDecode)) {
        $message_to_decode = str_replace('Content-Transfer-Encoding: quoted-printable', '', $the_mail);
        $letter_html = quoted_printable_decode($message_to_decode);
    }elseif(preg_match_all("/Content-Transfer-Encoding: base64/", $the_mail, $mailToDecode)){
        $message_to_decode = str_replace('\r', '', $the_mail);
        $message_to_decode = str_replace('\n', '', $message_to_decode);
        $message_to_decode = str_replace('Content-Transfer-Encoding: base64', '', $message_to_decode);
        $letter_html = base64_decode($message_to_decode);
        // $letter_html = $message_to_decode;
    } else {
        $letter_html = $the_mail;
    }
    // echo $letter_html;
} else {


    $mailParts = explode("\n\n", $email_body);


    foreach ($mailParts as $k => $v) {
        if ($k > 0) {
            $letter_html .= $v . "\n\n";
        }
    }

    if(preg_match_all("/Content-Transfer-Encoding: base64/", $email_body, $mailToDecode)){
        $message_to_decode = str_replace('\r', '', $letter_html);
        $message_to_decode = str_replace('\n', '', $message_to_decode);
        $letter_html = base64_decode($message_to_decode);

        // $letter_html = $message_to_decode;
    }

}


?>

调用附件和文本的功能

<?php if ($has_attachment) { ?>
        <li id="attach_info">
        <span class="glyphicon glyphicon-time grey"></span> 
        <strong><?=$lang[$xlang][5]?></strong>: <a target="_blank" href="site.com/<?=$attach_name[1][0]?>" data-target="<?=$id?>"><?=$attach_name[1][0]?></a>
        </li>
    <?php } ?>


    <div class="pm-text" id="view_body">
        <div data-x-div-type="html" >
        <div data-x-div-type="body" >
            <div dir="ltr" class="pm-message"><?=$letter_html?></div>
        </div>
        </div>      
      </div>

我仍然有附件文件

<?php
  if (isset($_GET['attachment'])) {
    # code...
  } 
?>

我不知道你是否理解我的问题。

此致

0 个答案:

没有答案