dongyukui8330 2013-10-24 16:45
浏览 29
已采纳

电子邮件附件和HTML内容错误

I am writing an email attachment script in PHP. I have successfully done the image attachment to email but I am facing issues with the html.

Two Important thing in the Email:

  1. Email Content which is HTML with some designs.

  2. Email Attach Images.

If i use the following header I am able to see the Email with HTML designed. But attachment not working.

$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=UTF-8
";

If i use the following header I can able to attach the images successful. but the html is just coming as it is not showing how it was designed...

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "This is a multi-part message in MIME format.

" . "--{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" . "Content-Transfer-Encoding: 7bit

" . $message . "

"; 

Also this email should be shown perfectly in Outlook:

Can some one help to solve the issue.

  • 写回答

1条回答 默认 最新

  • dongxiaoshe0737 2013-10-24 16:56
    关注

    The better way (easier and works fine) will be to use mailer class like PHPMailer...

    But to answer you, you can find some help here.

    You can find another helful example here

    <!-- language: lang-php -->
    // Prepare by setting a timezone, mail() uses this.
    date_default_timezone_set('America/New_York');
    
    // Save some values to send an email, these might have come from any source:
    $to = 'example@eliw.com';
    $subject = 'A sample email - Dual Format';
    
    // Create a boundary string.  It needs to be unique (not in the text) so ...
    // We are going to use the sha1 algorithm to generate a 40 character string:
    $sep = sha1(date('r', time()));
    
    // Define the headers we want passed.  Note that they are separated by 
    
    $headers = "From: php@example.com
    X-Mailer: Custom PHP Script";
    
    // Add in our content boundary, and mime type specification:
    $headers .=
        "
    Content-Type: multipart/alternative; boundary=\"PHP-alt-{$sep}\"";
    
    // The body of the message.  Use the separator with -- in front of it to
    //  mark the beginning of each section, and then provide the content type.
    //  A blank line beneath that will define the beginning of the content.
    //  At the end finish with the separator again, but this time with a --
    //  after it as well.
    $body =<<<EOBODY
    --PHP-alt-{$sep}
    Content-Type: text/plain
    
    This is our sample email message
    
    Hello World!
    
    That's it for now
    
    --PHP-alt-{$sep}
    Content-Type: text/html
    
    <p>This is our sample email message</p>
    <h2>Hello World!</h2>
    <p>That's it for now.</p>
    
    --PHP-alt-{$sep}--
    EOBODY;
    
    // Finally, send the email
    mail($to, $subject, $body, $headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用