MIME::Lite - Easily send emails
The MIME::Lite module makes it easy to send emails.
MIME::Lite uses the senmail command by default, so install sendmail or postfix .
There are several ways to send an email, but I think sending an email with the sendmail command is an easy way to deal with various email issues.
Send an email
To send an email: It's easy to create in the form of a multipart message and attach and send text or images.
use MIME::Lite; # Create multipart message my $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Cc =>'some@other.com, some@more.com', Subject =>'A message with 2 parts ...', Type =>'multipart/mixed' ); # Add text $msg->attach( Type =>'TEXT', Data => "Here's the GIF file you wanted" ); # Add image $msg->attach( Type =>'image/gif', Path =>'aaa000123.gif', Filename =>'logo.gif', Disposition =>'attachment' ); # Send email $msg->send;
- Create the content to be sent with the new method.
- Add data such as text and images with the atach method.
- Send an email with the send method.
Send Japanese email with MIME::Lite
When sending Japanese emails with MIME::Lite, you need to think about encoding.
Remember the following two things.
- The title (Subject) of the header part is encoded with MIME-Header-ISO_2022_JP .
- The text data of the text is encoded in ISO-2022-JP .
I'm wondering if UTF-8 encoding is fine in most cases, but it's safe to use the above encoding.
Save the source code in UTF-8 and enable the utf8 pragma at the beginning.
The above example can be rewritten for Japanese transmission as follows.
use utf8; use MIME::Lite; use Encode 'encode'; # Create multipart message my $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Cc =>'some@other.com, some@more.com', Subject => encode('MIME-Header-ISO_2022_JP', 'mail title'), Type =>'multipart/mixed' ); # Add text $msg->attach( Type =>'TEXT', Data => encode('ISO-2022-JP', 'text of email body'), ); # Add image $msg->attach( Type =>'image/gif', Path =>'aaa000123.gif', Filename =>'logo.gif', Disposition =>'attachment' ); # Send email " $msg->send;
When sending in UTF - 8
As of 2017, I'm not sure what's going on, but I feel that it's often sent in UTF-8, so I'll post an example of that as well.
use utf8; use MIME::Lite; use Encode 'encode'; # Create multipart message my $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Cc =>'some@other.com, some@more.com', Subject => encode('MIME-Header', 'mail title'), Type =>'multipart/mixed' ); # Add text $msg->attach( Type =>'TEXT', Data => encode('UTF-8', 'text of email body'), ); # Add image $msg->attach( Type =>'image/gif', Path =>'aaa000123.gif', Filename =>'logo.gif', Disposition =>'attachment' ); # Send email " $msg->send;
If you don't know the character code of Perl, see the explanation article of Encode module.
FAQ
Answer the FAQ about MIME::Lite.
MIME::Lite seems to be deprecated
MIME::Lite is easy to use, but is currently deprecated.
MIME::Lite is personally the easiest email sending module to use, but if you follow the community's wishes, Email::Sender.
Garbled characters occur
The cause is that there are characters that can be expressed in UTF-8 but not in "ISO-2022-JP".
In such a case, encode it in UTF-8 and send an email, or replace the characters expressed in UTF-8 with characters that can be converted to "ISO-2022-JP".