PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Mail » Mail_Mime » Manual

A Package to enable easy creation of complex multipart emails. If you look for a simple API for creating such emails, then Mail_Mime class will probably suffice. Else you can use Mail_mimePart, which gives you better control about MIME creation.

Mail_Mime::Mail_Mime()

Mail_Mime::Mail_Mime() – creates a new instance

Synopsis

require_once 'Mail/mime.php';

Mail_mime Mail_mime ( mixed $params = array() )

Description

Creates a new instance of Mail_Mime

Parameter

array $params - An associative array of parameters. These parameters affect the way the message is built. Use Mail_Mime::setParam() to set them later.

Note

Normally, it is not necessary to set parameters. But, if you want to send the generated MIME message using Mail then you have to set eol to "\n".

For backward compatybility setting end of line string as constructor's first parameter is supported.

If you're working with big attachments, enabling 'delay_file_io' will provent from loading attachments into memory. Until you're not using getMessage* functions don't worry about PHP's memory limit.

Mail_Mime::addAttachment()

Mail_Mime::addAttachment() – add attachment

Synopsis

require_once 'Mail/mime.php';

boolean addAttachment ( string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = true , string $encoding = 'base64' , string $disposition = 'attachment' , string $charset = '' , string $language = '' , string $location = '' , string $n_encoding = null , string $f_encoding = null , string $description = '' , string $h_charset = null )

Description

Adds an attachment to a message.

Parameter

Return value

boolean - Returns TRUE on success, PEAR_Error on failure.

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
NULL "File is not readable file_name" The file was not found or the script has not enough rights to access the file. Check the file name and path. Check user and file permissions.
NULL "Could not open file_name" The file is already opened and exclusivly locked by another application. In the most cases a program opens the file for writing. addAttachment() does no file locking, so this problem is not caused by competitive callings of this function.

Note

This function can not be called statically.

Mail_Mime::addHTMLImage()

Mail_Mime::addHTMLImage() – add image to message

Synopsis

require_once 'Mail/mime.php';

boolean addHTMLImage ( string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = true , string $content_id = null )

Description

If sending an HTML message with embedded images, use this function to add the image.

Parameter

Return value

boolean - Returns TRUE on success, PEAR_Error on failure.

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
NULL "File is not readable file_name" The file was not found or the script has not enough rights to access the file. Check the file name and path. Check user and file permissions.
NULL "Could not open file_name" The file is already opened and exclusivly locked by another application. In the most cases a programm opens the file for writing. addHTMLImage() does no file locking, so this problem is not caused by competitve callings of this function.

Note

This function can not be called statically.

See

Mail_Mime::setHTMLBody()

Mail_Mime::get()

Mail_Mime::get() – build the message

Synopsis

require_once 'Mail/mime.php';

string &get ( array $param = null , resource $filename = null , boolean $skip_head = false )

Description

This function should be called once you have added the text/html/images/attachments. It builds the message and returns it. It does not send it. To send what this function returns (in conjunction with the headers() -function) you would need to use the Mail::send()-function

Parameter

Return value

string - the body of the message

Note

This function can not be called statically.

For versions older than 1.6.0 Mail_Mime::get() has to be called before Mail_Mime::headers().

See

Mail_Mime::headers()

Mail_Mime::headers()

Mail_Mime::headers() – build the header lines

Synopsis

require_once 'Mail/mime.php';

array &headers ( array $xtra_headers = null , boolean $overwrite = false , boolean $skip_content = false )

Description

Returns an array with the headers needed to prepend to the email (MIME-Version and Content-Type). Please note that the function get() has to be called before calling headers().

Parameter

Return value

array - an associative array with the mime headers and the additional headers. The return value can directly passed to the second parameter of Mail::send().

Note

This function can not be called statically.

Mail_Mime::headers() has to be called after Mail_Mime::get().

See

Mail_Mime::get()

Mail_Mime::setHTMLBody()

Mail_Mime::setHTMLBody() – set HTML part

Synopsis

require_once 'Mail/mime.php';

boolean setHTMLBody ( string $data , boolean $isfile = false )

Description

Sets the HTML part of a message

Parameter

Return value

boolean - Returns TRUE on success, PEAR_Error on failure.

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
NULL "File is not readable file_name" The file was not found or the script has not enough rights to access the file. Check the file name and path. Check user and file permissions.
NULL "Could not open file_name" The file is already opened and exclusivly locked by another application. In the most cases a programm opens the file for writing. setHTMLBody() does no file locking, so this problem is not caused by competitve callings of this function.

Note

This function can not be called statically.

See

Mail_Mime::setTxtBody()

Mail_Mime::setTxtBody()

Mail_Mime::setTxtBody() – set plain text part

Synopsis

require_once 'Mail/mime.php';

boolean setTxtBody ( string $data , boolean $isfile = false )

Description

Sets the plain text part of a message

Parameter

Return value

boolean - Returns TRUE on success, PEAR_Error on failure.

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
NULL "File is not readable file_name" The file was not found or the script has not enough rights to access the file. Check the file name and path. Check user and file permissions.
NULL "Could not open file_name" The file is already opened and exclusivly locked by another application. In the most cases a programm opens the file for writing. setTxtBody() does no file locking, so this problem is not caused by competitve callings of this function.

Note

This function can not be called statically.

See

Mail_Mime::setHTMLBody()

Mail_Mime::encodeHeader()

Mail_Mime::encodeHeader() – encode header value

Synopsis

require_once 'Mail/mime.php';

string encodeHeader ( string $name , string $value , string $charset , string $encoding )

Description

Returns encoded header value as for RFC2047.

Parameter

Return value

string - Returns encoded header body (without a name)

Note

This function can not be called statically.

Mail_mimePart::Mail_mimePart()

Mail_mimePart::Mail_mimePart() – constructor

Synopsis

require_once 'Mail/mimePart.php';

Mail_mimePart Mail_mimePart ( string $body = '' , array $params = array() )

Description

Create a new Mail_mimePart object.

Parameter

Note

This function can be called statically.

Mail_mimePart::addsubpart()

Mail_mimePart::addsubpart() – add sub part to a MIME part

Synopsis

require_once 'Mail/mimePart.php';

resource &addSubPart ( string $body , array $params )

Description

Adds a sub part to the current MIME part and returns a reference to it

Parameter

Return value

resource - a reference to the added part

Note

This function can not be called statically.

Example

Add two attachments to a mail

<?php    
include 'Mail/mimePart.php';

...

$params['content_type'] = 'multipart/mixed';
$email = new Mail_mimePart('', $params);

// Here we add a text part to the multipart we have
// already. Assume $body contains plain text.

$params['content_type'] = 'text/plain';
$params['encoding']     = '7bit';
$text = $email->addSubPart($body, $params);

// Now add an attachment. Assume $contents is
// the contents of the attachment

$params['content_type'] = 'application/zip';
$params['encoding']     = 'base64';
$params['disposition']  = 'attachment';
$params['dfilename']    = 'example.zip';
$attach =& $email->addSubPart($contents, $params);

// Now build the email. Note that the encode
// function returns an associative array containing two
// elements, body and headers. You will need to add extra
// headers, (eg. Mime-Version) before sending.

$email = $email->encode();
$email['headers']['Mime-Version'] = '1.0';

...
?>

Mail_mimePart::encode()

Mail_mimePart::encode() – encode a mail

Synopsis

require_once 'Mail/mimePart.php';

array encode ( string $boundary = null )

Description

Encodes and returns the email

Parameter

Return value

array - an associative array containing two elements, body and headers. The headers element is itself an indexed array.

The key names are

Note

This function can not be called statically.

Mail_mimePart::encodeToFile()

Mail_mimePart::encodeToFile() – encode a mail

Synopsis

require_once 'Mail/mimePart.php';

array encodeToFile ( string $filename , string $boundary = null , boolean $skip_head = false )

Description

Encodes and saves the email into file. File must exist. Data will be appended to the file

Parameter

Return value

array - An associative array containing message headers or PEAR error object

Note

This function can not be called statically.

Mail_Mime - Example

Mail_Mime - Example – generation and sending of a MIME mail

Example

<?php

include 'Mail.php';
include 'Mail/mime.php' ;

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "\n";
$hdrs = array(
              'From'    => 'you@yourdomain.com',
              'Subject' => 'Test mime message'
              );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);

?>