Web www.gerd-tentler.de
Version 2.2 (released Jan. 14, 2014) [Download]

Usage

Include the MIMEMAIL class into your Perl script and create a new instance:
require 'MIMEMAIL.pm';
$mail = MIMEMAIL->new('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}[0] = 'path/to/file1';
$mail->{attachments}[1] = 'path/to/file2';
$mail->{attachments}[2] = 'path/to/file3';
...
When all settings are done, create the MIME mail:
$mail->create();
And finally send it to each recipient:
# recipient list can be an array...
@recipients = ('recipient1@email', 'recipient2@email', 'Recipient3 <recipient3@email>');
if(!$mail->send(@recipients)) { print $mail->{error}; }

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

Notes

If you include attachments and Perl 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.

You can put the e-mails into sendmail's 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 Perl 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
$mail->create();

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

# for each recipient...
while(($name, $address) = each(%recipients)) {

  # replace variables in subject line and body text
  $mail->{subject} =~ s/\[NAME\]/$name/g;
  $mail->{body} =~ s/\[NAME\]/$name/g;

  # send e-mail
  if(!$mail->send("$name <$address>")) { print $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.

Comments