Web www.gerd-tentler.de
Version 2.3 (released Sep. 27, 2015) [Download]


Include the MIMEMAIL class into your PHP script and create a new instance:
$mail = new MIMEMAIL("HTML");
Then set senderName, senderMail, subject, and optionally cc, bcc, replyTo, and priority:
$mail->senderName = "sender name";
$mail->senderMail = "sender@email";
$mail->subject = "This is the subject line";
The mail body either contains your message (plain text or HTML):
$mail->body = "Hello! This is a message for you.";
Or, if you want to send an already existing HTML file instead, just put its path into the mail body:
$mail->body = "path/to/file";
The HTML text will be parsed for images, scripts and stylesheets, and they will be included into your e-mail automatically.

You can also attach files to your e-mail:
$mail->attachments[] = "path/to/file1";
$mail->attachments[] = "path/to/file2";
$mail->attachments[] = "path/to/file3";
When all settings are done, create the MIME mail:
And finally send it to each recipient:
// recipient list can be an array...
$recipients = array('recipient1@email', 'recipient2@email', 'Recipient3 <recipient3@email>');
if(!$mail->send($recipients)) echo $mail->error;

// ...or a string with comma-separated values
$recipients = 'recipient4@email, recipient5@email, Recipient6 <recipient6@email>';
if(!$mail->send($recipients)) echo $mail->error;


If you include attachments and PHP can not locate the files e.g. because of a wrong path, they won't be sent. Same goes of course for images and stylesheets inside your mail body. So before starting a mailing you should first send the e-mail to an address of yours, or save the e-mail using the saveDir option, and check if everything works like it should.

If you are using sendmail as MTA, you can put the e-mails into its mail queue instead of sending them immediately by using the useQueue option. This is useful if you are sending lots of e-mails, because by using the queue PHP doesn't have to wait for each e-mail until it is actually sent, thus making the script much faster. The drawback is that by using the mail queue you leave it to the MTA when your e-mails are being sent. This can take minutes or even hours, depending on server load and queue size.

Maybe you ask yourself why you have to call create() explicitely before sending your e-mail with send(). Well, imagine this: You want to send the same e-mail to several recipients, but you also want to personalize it, e.g. by starting with "Dear Peter", "Dear Paul", etc. If you had to create the entire e-mail with all attachments etc. for each recipient again only because the name has changed, this would be a big waste of time and resources. Instead, you create it only once with a variable [NAME] and then replace [NAME] with each recipient's name before sending. Example:
$mail->subject = "Hello [NAME]!";
$mail->body = "Dear [NAME], this is a personal message for you.";

// create the MIME mail once

$recipients = array('Peter' => 'peter@somewhere.com',
                    'Paul'  => 'paul@somewhere.com',
                    'Mary'  => 'mary@somewhere.com');

// for each recipient...
foreach($recipients as $name => $address) {

    // replace variables in subject line and body text
    $mail->subject = str_replace('[NAME]', $name, $mail->subject);
    $mail->body = str_replace('[NAME]', $name, $mail->body);

    // send e-mail
    if(!$mail->send("$name <$address>")) echo $mail->error;
This works because the MIMEMAIL class saves the original subject line and body text when the MIME mail is created and restores it each time after sending. Please note that this variable replacement only works with the subject line and the body text, not with any other contents.