Programmer's Reference Guide

HTML メール

ファイルの添付

メールにファイルを添付するには createAttachment() メソッドを使用します。 Zend_Mail のデフォルト設定では、添付ファイルは base64 エンコードされたバイナリオブジェクト (application/octet-stream) として添付されます。この挙動を変更するには、 createAttachment() に追加のパラメータを指定します。

例1 ファイルを添付したメール

  1. $mail = new Zend_Mail();
  2. // メッセージを作成します...
  3. $mail->createAttachment($someBinaryString);
  4. $mail->createAttachment($myImage,
  5.                         'image/gif',
  6.                         Zend_Mime::DISPOSITION_INLINE,
  7.                         Zend_Mime::ENCODING_BASE64);

添付ファイル用の MIME パートを細かく制御するには、 createAttachment() の返す値を使用してその属性を変更します。 createAttachment() メソッドの返す値は Zend_Mime_Part オブジェクトです。

  1. $mail = new Zend_Mail();
  2.  
  3. $at = $mail->createAttachment($myImage);
  4. $at->type        = 'image/gif';
  5. $at->disposition = Zend_Mime::DISPOSITION_INLINE;
  6. $at->encoding    = Zend_Mime::ENCODING_BASE64;
  7. $at->filename    = 'test.gif';
  8.  
  9. $mail->send();

もうひとつの方法は、Zend_Mime_Part のインスタンスを作成して それを addAttachment() で追加するものです。

  1. $mail = new Zend_Mail();
  2.  
  3. $at = new Zend_Mime_Part($myImage);
  4. $at->type        = 'image/gif';
  5. $at->disposition = Zend_Mime::DISPOSITION_INLINE;
  6. $at->encoding    = Zend_Mime::ENCODING_BASE64;
  7. $at->filename    = 'test.gif';
  8.  
  9. $mail->addAttachment($at);
  10.  
  11. $mail->send();

HTML メール

Comments

Didn't understand what kind of data should be in variable $myImage
@Pavel--
$myImage should be a binary string. A binary string can be obtained with file_get_contents() method. Here is an example:

$myImage = file_get_contents('logo.png');
$at = $mail->createAttachment($myImage);
$at->filename = 'logo.png';
@Pavel--
$myImage should be a binary string. A binary string can be obtained with file_get_contents() method. Here is an example:

$myImage = file_get_contents('logo.png');
$at = $mail->createAttachment($myImage);
$at->filename = 'logo.png';
For multipart/mixed messages there is no difference of MIME part generated for attachment, in both case they are between mixed MIME boundary:
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
and $at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
so in Thunderbird html mail with embedded images are not displayed.

According to this art. http://www.phpeveryday.com/articles/PHP-Email-Using-Embedded-Images-in-HTML-Email-P113.html and some wiki art., for embedded images, Zend_Mime::DISPOSITION_INLINE must place image to be part of text/html related MIME boundary, wich is not case.
Is It a Bug or ?

Thank You
@Ivan: If I understand you correctly, I've implemented the missing functionality in ZF-10970
Hi All
khi tôi làm thế này mà vẫn khong send được mail không biết là có lỗi gì hoặc sai ở đâu ?

when I do this and still not send the mail does not know what error or wrong?

$at = new Zend_Mime_Part('/hoc/email/trunk/public/upload/uploads/GsNJNwuI-UM.gif');
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->filename = '/hoc/email/trunk/public/upload/uploads/GsNJNwuI-UM.gif';
$mail->addAttachment($at);

help me

Hi All
I have found a method and all references. The following is an example

$myImage = file_get_contents('upload/uploads/avatar.jpg');
$at = new Zend_Mime_Part($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->filename = 'avatar.jpg';
$mail->addAttachment($at);

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual