【问题标题】:PHP Attaching an image to an emailPHP 将图像附加到电子邮件
【发布时间】:2010-10-06 21:58:02
【问题描述】:

有没有办法将图像附加到用 PHP 创建的 html 格式的电子邮件中?

我们需要确保在发送给在阅读电子邮件时可能无法访问互联网的客户的电子邮件中包含公司徽标(他们显然可以下载文件)。

【问题讨论】:

    标签: php email attachment


    【解决方案1】:

    试试 PEAR Mail_Mime 包,可以embed images for you

    您需要使用addHTMLImage() 方法并传递内容ID (cid),这是一个唯一的文本字符串,您还将在img 的src 属性中使用cid: URL。例如:

    include('Mail.php');
    include "Mail/mime.php";
    
    
    $crlf = "\r\n";
    $hdrs = array( 
            'From' => 'foo@bar.org', 
            'Subject' => 'Mail_mime test message' 
            ); 
    
    $mime = new Mail_mime($crlf); 
    
    //attach our image with a unique content id
    $cid="mycidstring";
    $mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, $cid);
    
    //now we can use the content id in our message
    $html = '<html><body><img src="cid:'.$cid.'"></body></html>';
    $text = 'Plain text version of email';
    
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html); 
    
    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
    
    $mail =& Mail::factory('mail');
    $mail->send('person@somewhere.org', $hdrs, $body);
    

    【讨论】:

    • 该死,你快了 20 秒 ;-)
    • 我得到的错误是Notice: Undefined property: Mail_mime::$_html_images in ... 的两倍。这是您声明 $cid 变量的行。它发送电子邮件,但不显示图像。你能更新你的答案吗?
    • 这个答案 7岁!如果您查看 Mail_Mime 的源代码,您会看到我访问的 $_html_images 成员已重命名为 $html_images 并受保护 github.com/pear/Mail_Mime/blob/1.10.0/Mail/mime.php#L111 从 Mail_mime 派生一个新类并添加一个方法给您并不难cid 的值。
    • 尝试编辑此答案,但被拒绝... Mail_Mime 模块允许您使用 addHTMLImage()[1] 指定内容 ID。因此,无需将其捞出,只需执行以下操作: $mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, "mycidstring"); $html = ''; [1]pear.php.net/manual/en/package.mail.mail-mime.addhtmlimage.php
    • 我没有拒绝它 :( 编辑对我来说很好,所以我改变了答案。非常感谢。
    【解决方案2】:

    使用一些可以处理电子邮件附件的库可能是最简单的。例如,PEAR 的Mail_Mime

    【讨论】:

      【解决方案3】:

      PEAR 的 Mail_Mime 包就是你想要的。

      设置好消息后,添加附件非常简单:

      $mime = new Mail_mime("\n");
      
      $mime->setTXTBody($msg_text);
      $mime->setHTMLbody($msg_html);
      
      // Add gif as attachment to mail
      $mime->addAttachment("/path/to/image/smile.gif", "image/gif");
      
      $body = $mime->get();
      $headers = $mime->headers($headers);
      $mail->send("joe@bloggs.com", $headers, $body);
      

      如果您希望自己的徽标显示在电子邮件的特定位置 - 而不仅仅是作为附件 - 您可以执行以下操作:

      // In your message html:
      <img src='logo.gif' alt='Our logo' />
      
      // PHP:
      $mime->addHTMLImage('/path/to/image/logo.gif');
      

      根据您用户的邮件客户端,这种方法可能会产生不同的结果,因此在发送之前尝试在虚拟 gmail、yahoo 和 hotmail 帐户上测试您的格式。

      【讨论】:

        【解决方案4】:

        您是自己滚动还是使用 预制类?我推荐PHP Mailer[0] 我自己,还有 PEAR::Mail_Mime[1] 等 Google 很乐意为您提供帮助 找。我一直在使用 PHP Mailer 发送带有嵌入图像的消息[2] 多年来没有任何障碍,尽管熊 请记住,每张图片都会增加 电子邮件的带宽权重很大,所以 一般不应该用于 散装的任何东西。为了呼应比尔, 也使用纯文本替代方案。

        [0]http://phpmailer.sourceforge.net/

        [1]http://pear.php.net/manual/en/package.mail.mail-mime.php

        [2]http://phpmailer.sourceforge.net/docs/PHPMailer/PHPMailer.html#AddEmbeddedImage

        取自http://lists.evolt.org/archive/Week-of-Mon-20060612/183029.html

        【讨论】:

          【解决方案5】:

          这里有足够多的答案可以帮助解决您的具体问题,但我只是认为可能值得指出的是,您可能有一个更大的问题,您没有考虑过。

          特别是 - 编写通过 PHP 发送的电子邮件充满了潜在的陷阱,只有在您非常清楚可能出现的问题时才应该这样做。

          如果您计划相当密集地发送电子邮件,我强烈建议您通过专门的电子邮件营销客户端或实施为您发送电子邮件的众多电子邮件营销 API 之一。 (mailchimp 显然是一个不错的选择)。

          【讨论】:

            【解决方案6】:

            在这里试用 swiftmailer 是一个很好的例子,如何使用嵌入图像http://swiftmailer.org/wikidocs/v3/embedding_images?s[]=embed

            【讨论】:

              猜你喜欢
              • 2016-12-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-12-07
              相关资源
              最近更新 更多