Mail_Mime (Previous) (Next) Mail_mimeDecode - Example

View this page in Last updated: Sun, 28 Sep 2008
English | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Plain HTML

Mail_Mime - Example

Mail_Mime - Example -- génération et envoi d'un courrier MIME

Exemple


<?php
include('Mail.php');
include('Mail/mime.php');

$text 'Version texte du courriel';
$html '<html><body>Version HTML du courriel</body></html>';
$file '/home/jean/exemple.php';
$crlf "\n";
$hdrs = array(
              'From'    => 'vous@votredomaine.com',
              'Subject' => 'Test message MIME'
              );

$mime = new Mail_mime($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);
?>

Mail_Mime (Previous) (Next) Mail_mimeDecode - Example

Download Documentation Last updated: Sun, 28 Sep 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: wiesemann
@buddy..., the send() method belongs to the Mail package, not to the Mail_mime package. If you look into the documentation for Mail, you'll find out that send() returns a PEAR_Error instance in case of an error. Therefore, the correct error checking is like this:
$res = $mail->send(...);
if (PEAR::isError($res)) {
// error
} else {
// success
}

=> http://pear.php.net/manual/en/package.mail.mail.send.php
Note by: buddy@databoysoftware.com
I notice that no where do I find simple error handling.
Correct me if I am wrong, but I think this should work:
if($mail_object->send($recipients, $headers, $message))
{
it worked
}
else
{
it didn't work
}
Note by: consulting@arlyle.com
It's a common mistake to think that "\n" is sufficient, BUT IT IS NOT! That's because the SMTP standard specifies that all lines MUST be terminated by "\r\n", not just "\n" or just "\r". Some SMTP daemons have a problem if you only use "\n".

That's why the default behavior of the class is to use "\r\n" and it really shouldn't be overridden.
Note by: Deger
I couldn't come accross to any smtp method examples and it took me a while to learn how to use this package. I hope this message ripens this pear package end-user manual a wee bit more making it ready to eat, straight from branch.

SMTP Example:
Please Note:
Auth_SSL is a dependent package here. You may get it like this: pear install --alldeps Mail_Mime-1.4.0.tgz

<?php
$your_html_message 
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Hello World!</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>'
;
// Require Pear Mail Packages
require_once ("Mail.php");
require_once (
"Mail/mime.php");
$recipients  'Deger <Deger@bogusdomain.con>';
// Additional headers
$headers["From"] = 'Deger <Deger@bogusdomain.con>';
$headers["To"]    = 'Deger <Deger@bogusdomain.con>'
$headers["Subject"] = "Ready To Eat Pears";
$crlf "\n";
$mime = new Mail_mime($crlf);

$mime->setHTMLBody($your_html_message);
$message $mime->get();
$headers $mime->headers($headers);
$params["host"]    = 'mail.bogusdomain.con';
$params["auth"]    = TRUE// note: there are *no delimiters*

// note: there are *no delimiters* for DIGEST-MD5 either. 
// If you want to use PLAIN,
// you have to use delimiters like this: 'PLAIN'
$params["auth"]    = DIGEST-MD5
$params["username"]    = 'yourusername';
$params["password"]    = 'yourpassword';
$params["localhost"]= 'This.may.be.yourIP';
// Debug so that we see what's happenning for the moment.
$params["debug"]    = "True"
// create the mail object using the Mail::factory method
$mail_message =& Mail::factory('smtp'$params);
$mail_message->send ($recipients$headers$message);
?>
Note by: jb@fusionquest.com
With regard to $mime->headers($hdrs);...

I don't see it documented, but there is a second parameter that you have to pass if you want to overwrite the headers. For example, your script sends one email. Then, you want to send another email to someone else with different subject line. By default, mime will use the previous headers sent. If you want to overwrite them, you need to do this:
$mime->headers($hdrs, true);
Note by: josh@ionexinteractive.com
There's a difference between '\n' and "\n". With single quotes, PHP interprets that as a LITERAL string. Double quotes are for INTERPRETED strings.

Thus, passing '\n' spits out exactly \n.

Passing "\n" spits out the actual carriage return/line feed code needed to create a new line.

If you want a single line separator (like one <br />), just pass one "\n". If you want a blank line space between things (i.e. <br /> <br />), pass "\n\n". And so on for three, four, five, whatever lines.

Usage example:
$message = 'Hello';
$message .= "\n\n".'World!';

require_once('Mail.php');
require_once('Mail/mime.php');

$crlf = "\n"; //note the DOUBLE quotes.
$hdrs = array (
'From' => 'Somebody <no-reply@example.com>',
'Subject' => 'Some Subject.');
);

$mime = new Mail_mime($crlf);
$mime->setTXTBody($message);

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

$mail =& Mail::factory('mail');
$mail->send('foobar@example.com', $hdrs, $body);

This will send you an e-mail that looks like this:

Hello

World!
Note by: nobody
Re: comment from rolf.offermanns@*

Note that '\n' is different from "\n".
Note by: rolf.offermanns@gmx.net
Passing '\n' for the crlf parameter in the Mail_mime constructor does not work for me and produces invalid mails.

Omitting the parameter (thus using the default value) works fine!