NAME Mail::Builder::Simple - Send UTF-8 HTML and text email with attachments and inline images, eventually using templates SYNOPSIS # Send a plain text email with Sendmail: use Mail::Builder::Simple; my $mail = Mail::Builder::Simple->new; $mail->send( from => 'me@host.com', to => 'you@yourhost.com', subject => 'The subject with UTF-8 chars', plaintext => "Hello,\n\nHow are you?\n", ); # Send the email with an SMTP server: $mail->send( mail_client => { mailer => 'SMTP', mailer_args => [Host => 'smtp.host.com'], }, from => 'me@host.com', to => 'you@yourhost.com', subject => 'The subject with UTF-8 chars', plaintext => "Hello,\n\nHow are you?\n", ); # Send a text and HTML email with an attachment and an inline image # Specify the displayed name for To: and From: fields and add other headers $mail->send( from => ['me@host.com', 'My Name'], to => ['you@yourhost.com', 'Your Name'], reply => 'foo@anotherhost.com', subject => 'The subject with UTF-8 chars', plaintext => "Hello,\n\nHow are you?\n\n", htmltext => "
How are you?
", attachment => 'file.pdf', image => 'logo.png', priority => 1, mailer => 'My Mailer 0.01', ); DESCRIPTION "Mail::Builder::Simple" can create email messages with Mail::Builder and send them with Email::Send. It has the following features: UTF-8 encoding "Mail::Builder::Simple" automaticly encodes the body and headers of the email messages to UTF-8, so they can display the special chars in other languages than English correctly. attachments "Mail::Builder::Simple" allow adding one or more attachments to the message, without needing to specify their Content-Type if you don't want to. The attachments can be files saved on the disk or can be created on the fly, eventually using templates. images "Mail::Builder::Simple" can add inline images that will be displayed in the HTML part of the message. templates The body and the attachments can be created using a template, either using external template files or templates from scalar variables. "Mail::Builder::Simple" uses other modules like Mail::Builder::Simple::TT and Mail::Builder::Simple::HTML::Template in order to allow using Template or HTML::Template templates. For using another templating system, you can create a module like these 2 modules. mail sender "Mail::Builder::Simple" can send the email messages using any of the mailers allowed by Email::Send and some of them are: Sendmail, Qmail, SMTP, Gmail, NNTP. Some of these mailers might not be offered by the Email::Send main distribution, and you might need to install separate modules from CPAN (Such an example is Email::Send::Gmail.) configuration file All the parameters that can be sent to the "new()" function can be also stored in a configuration file, and this file can be used in more applications. For example, you could save the mailer type, the mailer host, username and password and maybe the "From:" field in a configuration file, so you won't need to specify them each time when you want to send an email. Functions Mail::Builder::Simple offers the following functions: new() This is the constructor of the "Mail::Builder::Simple" object. This object is a Mail::Builder object also, so you can use the methods from Mail::Builder on it if you want. send() This function sends the email. After sending the email, it cleans the "To:", "CC:" and "BCC:" fields, so you can send the already built message to somebody else if you want, needing to specify only the recipient's email address. These 2 functions can receive a hash with parameters which have the keys explained below. Parameters mail_client This parameter is optional. It is a hashref with all the options needed to configure Email::Send for sending the email messages. It looks like: mail_client => { mailer => 'SMTP', mailer_args => [Host => 'smtp.host.com'], }, where the mailer can be 'SMTP', 'Sendmail', 'Qmail', 'Gmail', 'NNTP', or other types supported by Email::Send. "mailer_args" receives all the configuration options that might be required by the specified mailer. For example, for sending email with an SMTP host that require authentication, you should use: mailer_args => { mailer => 'SMTP', mailer_args => [ Host => 'smtp.host.com', username => 'the_user', password => 'the_password', ], }, If the parameter "mail_client" is not specified, the default mailer that is used is sendmail. template_args This parameter is optional. It is a hashref with all the arguments neede by the templating system you are using for creating the email body or the attachments. "template_args" can receive any kind of parameters, depending on the parameters which are accepted by the templating system used. "Mail::Builder::Simple" allows using more templating systems even for creating a single email message. If more templating systems are used to create an email message, all the templates will use the arguments from the hashref "template_args" unless overwritten, as you will see. It could look like: template_args => { INCLUDE_PATH => '/path/to/templates', #default "." ENCODING => 'UTF-8', #The default is UTF-8 anyway }, template_vars This parameter is optional. It is a hashref with the pairs of variables from the templates and their values. An example: template_vars => { name => 'Gil Bates', preferences => ['pizza', 'yogurt', 'blondes'], }, The variables from "template_vars" will be used by all the templates which are used for creating the email message, unless some of them are overwritten as you will see. email message fields These are the fields that create the email message. They are: "from, to, cc, bcc, subject, plaintext, htmltext, attachment, image, priority, reply, organization, returnpath, sender, language, mailer". There are many ways of using these fields, and I will explain them below. config_file This parameter is optional. It shows the path to a configuration file that holds some parameters you don't want to specify in each program. The configuration file can be any type of file supported by Config::Any: Apache config style (Config::General), JSON, INI files, XML, YAML or perl code. Here is an example of a configuration file that uses Config::General: (/home/user/email.conf)How are you?
", attachments The attachments can be added as parameters to "new()" and "send()" methods. Attach a file without specifying an alternative name and its Content-Type: attachment => 'file.pdf', Attach a file specifying an alternative name and its Content-Type: attachment => ['/path/to/file', 'filename.pdf', 'application/pdf'], Attach more file without specifying alternative names and Content-Type in 2 ways: attachment => ['MORE', 'file1.pdf', 'file2.doc'], or attachment => [['file1.pdf'], ['file2.doc'], ['file3.html']], Attach more files specifying their alternative names and Content-Type: attachment => [ ['file1', 'file1.pdf', 'application/pdf'], ['file2', 'file2.pdf', 'application/pdf'], ], or attach files using methods of the "Mail::Builder::Simple" object. You can repeat this for more times: $mail->attachment->add('file1.pdf'); or $mail->attachment->add('file', 'file.pdf', 'application/pdf'); images "Mail::Builder::Simple" allows attaching inline images that won't appear as attachments, but they will be displayed in the HTML part of the mail message. You can add them as parameters to the "new()" or "send()" functions. Add an inline image without specifying an alternative ID: image => 'image.png', Add an inline image and specify an alternative ID: image => ['/path/to/image.png', 'image_id'], Add more inline images without specifying an alternative ID: image => ['MORE', 'image1.png', 'image2.gif', 'image3.jpg'], or image => [['image1.png'], ['image2.gif'], ['image3.gif']], Add more inline images specifying an alternative ID: image => [ ['/path/to/image1.png', 'logo'], ['image2.gif', 'img'], ['image3.jpg', 'picture'], ], or you can add them using methods of the "Mail::Builder::Simple" object. You can repeat it for more times: $mail->image->add('image.png'); or $mail->image->add('/path/to/image.png', 'logo'); Only the .png, .jpg and .gif images can be attached as inline images. The ID of the image is used for displaying the image in the HTML part of the email message, using something like the following HTML element for the "logo" ID: If you don't provide an ID, one is automaticly generated and it will be the lowercase of the file name of the images, without the extension. Using templates "Mail::Builder::Simple" allows to create the text and HTML body of the email message or the attachments using templates. When the value of the parameters "plaintext", "htmltext" and "attachment" is an arrayref and the last element of that arrayref begins with ":", it means that this field is created using a template. The type of template is specified in that last element of the array. Types of templates "Mail::Builder::Simple" uses other plugin modules like Mail::Builder::Simple::TT and Mail::Builder::Simple::HTML::Template for creating the content using Template or HTML::Template. If you want to create the content using a templating system for which there isn't a plugin created yet, you can create that plugin. It is pretty simple. The templates that can be used for the moment are: Scalar TT TT-scalar HTML::Template HTML::Template-scalar Here are a few examples for creating a message plain text body using templates: # Create the plain text part of the email message using the TT template file "template.tt" my $mail = Mail::Builder::Simple->new; $mail->send( from => 'me@myhost.com', to => 'you@host.com', subject => 'The subject', plaintext => ['template.tt', ':TT'], template_args => { INCLUDE_PATH => '/path/to/templates', }, template_vars => { name => 'My Name', age => 20, }, ); and the template file in /path/to/templates/template.tt could contain: Hello [% name %], My age is [% age %]. # Create the plain text part of the email message using a TT template from a scalar variable my $template = <