自動郵件程式 perl

2021-07-09 11:46:21 字數 3286 閱讀 5631

#!/usr/bin/perl

=pod

author@laomeng

e_mail:[email protected]

運維工作自動化:

由於這種工作是一種重複性的操作,在人工產生的報表過程有可能出現

資料的錯或格式不正確通過(難免不認真),所以為減少工作的重複性和避

免生產過程中資料錯誤和格式錯誤而開發了自動郵件程式(這只是主程式一部分)。

=cut

use mime::base64;

use encode;

use mime::lite;

use io::handle;

use posix qw(strftime);

my$ydate= strftime("%y-%m-%d", localtime(time - 24

*3600));#yestarday

my$file='/home/mamc_report/report_file/mamc_core_report$ydate.xls';

my$str="example呼叫報表[($ydate)]介面";

use dbi;

use encode;

use file::find;

use data::dumper;

#資料賬戶資訊

my%mysql = (

'dbname' => "example", #資料庫名

'host' => "127.0.0.1",

'port' => 3306,

'user' => 'example', #使用者

'pass' => 'example', #密碼

);#刪除之前的資料檔案

unlink

'./mamc.html'

if -e './mamc.html';

#建立新的資料檔案

open(fl,'>','/home/mamc_report/mamc.html') or

die"can't write file:$!\n";

my@data;

my$database ="dbi:mysql:$mysql";

my$dbh = dbi->connect($database,$mysql,$mysql) or

die("could not make connection to database:$dbi::errstr");

$dbh->do("set character_set_client='utf8'"); #連線客戶端編碼

$dbh->do("set character_set_connection='utf8'"); #連線程式設計

$dbh->do("set character_set_results='utf8'"); #查詢結果編碼

#作為查詢條件和小標籤

my@api=("test1","test2","test3","test4","test5","test5");

print fl "\n";

print fl '',"\n";

print fl "\n";

print fl '各位領導、同事好:

',"\n";

print fl '  以下是example介面呼叫報表,a&b的top5統計圖見附件

',"\n";

print fl "\n";

my$num;

foreach

my$name (@api)

}print fl "\n";

print fl "\n";

print fl "\n";

=pod

讀取郵件報表內容,由於perl5.10.1版本的bug

導致open函式或其模組(讀取檔案)輸出的結果

不完整。由此改用linux帶的cat命令開啟檔案

內容賦值給@str陣列。建議在perl新版本5.20

以後的版本

#use tie::file;

#my @str = slurp::to_array("./mamc.html");

#tie @str, 'tie::file',"./mamc.html";

=cut

#開啟報表資料內容

my@str = qx(cat /home/mamc_report/mamc.html);

my$mail = mime::lite->new(

from =>

'[email protected]', #發郵件人位址

to =>

'[email protected]', #收件人位址

cc =>

'[email protected];[email protected]', #抄送郵件人位址

subject =>

"$str", #郵件主題

type =>

'multipart/mixed',

);#新增郵件附件

$mail->attach(

type =>

'image/svg', #檔案型別

id =>

'line.svg',

path =>

'./line.svg', #檔名

);#報表資料內容

$mail->attach(

htmlcharset =>

'utf-8', #編碼設定

type =>

'text/html', #檔案型別

data => \@str

#郵件正文

);#添郵件附件

$mail->attach(

htmlcharset =>

'utf-8',

textcharset =>

'utf-8',

textencoding =>

'base64',

htmlencoding =>

'base64',

type =>

'auto',

path =>

"$file", #附件檔案

disposition =>

'attachment'

);#smtp伺服器位址或網域名稱,timeout設定超時時間, debug引數0關閉,1開啟

$mail->send('smtp','example.example.com.cn',timeout=>

60,debug=>

1);

Perl傳送郵件

本文簡單的介紹一下使用perl傳送郵件的方法,我們以使用163的smtp伺服器來傳送郵件為例,因為需要使用者驗證,所以前提是我們有163的郵件賬戶。如果 中使用到的perl模組你沒有安裝,那麼執行的時候就會報錯,關於perl模組的安裝方法,參照這裡。usr bin perl w use strict...

perl傳送郵件

在外地出差也搞什麼郵件簽到,真無聊 前一段看歐錦賽,時差還沒調整過來,早上起不來zzzzz 不過上有政策,下有對策嘛 就在伺服器上寫了乙個perl指令碼,用net smtp發郵件,用crontab來定時跑,一下就解決了,哈哈 perl指令碼 usr bin perl use net smtp use...

perl發郵件指令碼

在外地出差也搞什麼郵件簽到,真無聊 前一段看歐錦賽,時差還沒調整過來,早上起不來zzzzz 不過上有政策,下有對策嘛 就在伺服器上寫了乙個perl指令碼,用net smtp發郵件,用crontab來定時跑,一下就解決了,哈哈 perl指令碼 123 4567 891011 1213 1415 161...