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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值