【问题标题】:PHP mail opens in Gmail but not in OutlookPHP 邮件在 Gmail 中打开,但在 Outlook 中不打开
【发布时间】:2013-03-10 09:26:21
【问题描述】:

当我使用 PHP 发送邮件时,它在 Gmail 中显示正确,但在 Outlook 中显示不正确。这是一封带有 PDF 附件和一些文本的邮件。 PDF 使用 fpdf 创建并作为附件发送,这在 Gmail 和 Outlook 中运行良好。

唯一的问题是,在 Gmail 中,电子邮件的文本显示正确,而在 Outlook 中,它只是一个空白页,文本是一个额外的附件。

这是我的代码:

// encode data 
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// email stuff (change data below)
$to = $contactEmail; 
$from = $myEmail; 
$subject = "MySubjectHere"; 
$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
            <html><head></head><body>
            <p>Hi '$contactName.',</p><br>
            <p>Please find your pdf attached per your request.</p>
            <p>Feel free to call or email if you have any questions.</p>
            <p>Kind regards,</p><br>
            <p>MyNameHere</p>
            <p><strong>Manager</strong></p>
            <img alt="image" src="http://LinkToImage.png"/>
            <p style="color:#76923c;">
            <strong>P:</strong> 01 2345 6789| 
            <strong>W:</strong> <a href="http://www.example.com" target="_blank">www.example.com</a></p>
            <p style="color:#76923c;">
            <strong>A:</strong> 94 Test Street. 
            </p></body></html>';

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = $id."_".str_replace(" ","_",$Name).'.pdf';

// main header
$headers  = "From: ".$from.$eol;
$headers .= "Bcc: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
mail($to, $subject, $body, $headers);

【问题讨论】:

  • 我有一个问题。您将如何使用 php 向 Outlook 发送和接收电子邮件?谢谢
  • 是的,你会的。您只需要确保标头正确并且消息末尾有 2 个 end of line 。如果您的结构正确,您将能够查看所有客户端中的邮件。
  • 怎么样?我尝试向 Outlook 发送电子邮件,但没有收到任何内容。我应该把SMTP服务器和端口吗?
  • 确保您的邮件服务器设置正确:)

标签: email outlook gmail php


【解决方案1】:
$separator = md5(time());
$eol = "\r\n";
$message="";  // Always empty
$mailmessage="<h1>Yes got it</h1>";  //html content

$headers = "From: ".$email.$eol;
$headers .= "Bcc:". $Bcc. "\n";
$headers .= "Cc:". $cc. "\n"; 
$headers .= "Return-Path: <".$email.">\n";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;

$body .= "--" . $separator . "\r\n";
$body .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n";
$body .= $message . "\r\n";

$body = "--" . $separator . $eol;
$body .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $mailmessage . $eol.$eol;

$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol.$eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

mail($to, $subject, $body, $headers);

【讨论】:

    【解决方案2】:

    我发现哪里出错了:

     $body .= $message.$eol;
    

    应该是

     $body .= $message.$eol.$eol;
    

    3天后终于找到了!
    无论如何感谢您的回复!

    【讨论】:

      【解决方案3】:
      $to = $contactEmail; 
      $from = $myEmail; 
      $boundary = md5(date('r', time()));
      $msg  = "This is a multi-part message in MIME format.\n\n";
      $msg .= "--$boundary\n";
      $msg .= "Content-Type: text/html; charset=utf-8; format=flowed; delsp=yes\n";
      $msg .= "Content-Transfer-Encoding: 7bit\n\n";
      $msg .= "<h1>Website Enquiry</h1>
                  <p><strong>Name:</strong> $name<br />
                  <strong>Email:</strong> $email<br />
                  <strong>Phone:</strong> $phone<br />
                  <strong>Subject Name:</strong> $subject<br />
                  <strong>Question:</strong> $question</p>";
      
      $e_headers = "From: " . $from . "\n";
      $e_headers .= "MIME-Version: 1.0\n";
      $e_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
      
      mail($to, $subject, $msg, $e_headers);
      

      这是我将用于 HTML 联系表单的代码。 您不需要通过 PHP_EOL 传递内容,只需通过附件即可

      【讨论】:

        猜你喜欢
        • 2020-04-22
        • 2011-08-17
        • 1970-01-01
        • 2023-01-16
        • 1970-01-01
        • 2017-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多