As a software developer, sometimes some processes on the server need to be monitored to understand the running status of the server. Therefore, when an exception occurs, the email prompt is a good choice, which enables developers to perform maintenance in a timely manner.
1. Use mail: Sendmail to send a common email.
#! /Usr/bin/perl
Use strict;
Use mail: Sendmail;
Sendmail
(
From => '2014 @ gmail.com ',
# When using QQ or 163 as the mail address, you need to set the 123456@gmail.com to the White List (otherwise QQ will think it is spam and rejection, 163 do not need to set the White List); test Gmail, emails that Gmail considers to be insecure will be rejected
To => '2017 @ QQ.com ',
Subject => 'waring ',
Message => "something bad, pls check !! ",
);
Note: When Perl prompts that the module does not exist, use CPAN mail: Sendmail to install it. When an email fails to be sent, check the/var/spool/mail/root log to find out the cause of the failure.
2: use MIME: Lite to send emails with attachments
#! /Usr/bin/perl
Use strict;
Use MIME: lite;
# First, create a MIME: Lite object that contains the mail header information:
My $ MSG = MIME: Lite-> New (
From => '2014 @ gmail.com ',
To => '2017 @ QQ.com ',
Subject => 'warning ',
Type => 'multipart/mixed'
);
# Then add the attachment content using the attach method:
$ MSG-> attach (
Type => 'auto ',
Path => '/usr/local/sbin/SS. pl', # attachment path
);
Mime: The parameter type of the lite module determines the attachment type and how to add the attachment:
Path specifies the path of the file to be attached.
Filename specifies the default file name of the attachment when the receiver saves the attachment. If the path parameter is specified, the default file name is the name in the path.
Data specifies the date on which the attachment is added
Type specifies the file encoding type of the attachment to be added.
Disposition: The value can only be inline and attachment. the former specifies that the content of the attachment will be displayed after the body of the email when the recipient opens the email, instead of being an additional object. the latter specifies that the receiver should specify the decoding method of an attachment and save the attachment. A prompt is displayed.
$ MSG-> send (); # the default method is to use sendmail rules to send
# $ MSG-> send ('smtp ', 'mailserver .example.com', timeout => 30, DEBUG => 1); # specify other methods
You can use MIME: Lite to send emails using sendmail or net: SMTP.
When calling the send method, if the first parameter is "SMTP", Use Net: SMTP to send the mail. other parameters of send are passed to net: SMTP. if no value is added, it is the system's Sendmail.