Perl automatic mail

Source: Internet
Author: User
Tags email account perl script
Perl script:

#! /Usr/bin/perl
Use Net: SMTP;
Use MIME: base64;

################
# Automatic sign-in script #
################

$ Host = '10. 182.131.153 '; # SMTP server address

###########################
#
# $ HOST: SMTP Server
# $ Auth: email account
# $ Password: email account password
# $ To: target to be sent
# $ Mail_body: email content
#
###########################

Sub send_mail {
My ($ host, $ auth, $ password, $ to, $ mail_body) = @_;
My $ SMTP = net: SMTP-> New (
Host => $ host,
Hello => $ host,
Timeout => 30,
DEBUG => 1
);

$ SMTP-> auth (substr ($ auth, 0, index ($ auth, '@'), $ password );
$ SMTP-> mail ($ auth );
$ SMTP-> to ($ );
$ SMTP-> bcc ($ auth );
$ SMTP-> data ();
$ SMTP-> datasend ("Content-Type: text/plain; charset = gb2312 \ n ");
$ SMTP-> datasend ("content-transfer-encoding: base64 \ n ");
$ SMTP-> datasend ("from: $ auth \ n ");
$ SMTP-> datasend ("to: $ to \ n ");
$ SMTP-> datasend ("Subject: =? Gb2312? B? ". Encode_base64 ($ mail_body ,'')."? = \ N ");
$ SMTP-> datasend ("\ n ");

$ SMTP-> datasend (encode_base64 ($ mail_body, '')." \ n ");
$ SMTP-> dataend ();
$ SMTP-> quit;
}

# Obtain command line parameters
If (@ argv <1 ){
$ Conf_file = './mailusers. conf'; # Default Configuration File
}
Else {
$ Conf_file = $ argv [0]; # Get the configuration file name
}

# Open the configuration file and Log File
Open conf_file, $ conf_file or die "Open config file [$ conf_file] failed! \ N ";
Open LOG_FILE, '> send. log' or die "open send. log failed! $! \ N ";
While (<LOG_FILE> ){
Chomp;
If ($ _ = ~ /^ # + /){
Next; # Skip the comment line
}

@ Line = Split/\ s +/, $ _;
If (@ line! = 4 ){
Next; # Skip empty rows
}

# Send an email
Send_mail ($ host, $ line [0], $ line [1], $ line [2], $ line [3]);
Print LOG_FILE "[". localtime (). "] send_mail ($ host, $ line [0], $ line [1], $ line [2], $ line [3]); \ n ";
}

Close conf_file;
Close LOG_FILE;

The account and target of the automatic mail are configured in the configuration file. The configuration file format is mails. conf:

# <Auth> <password> <to> <mail body>
000000 sign in and then configure automatic run time in crontab:

# Hour, day, month, and week commands
41 8 ** 1-5/usr/bin/perl/home/AIG/. Run/checkin. pl/home/AIG/. Run/mails. conf
58 13 ** 1-5/usr/bin/perl/home/AIG/. Run/checkin. pl/home/AIG/. Run/mails. conf

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.