| 订阅 | 在线投稿
分享
 
 
 

在Perl中使用SendMail发送邮件

2008-05-19 06:25:52 编辑來源:互联网 国际版 评论
 
 
本文为【在Perl中使用SendMail发送邮件】的汉字拼音对照版显示拼音
  gaowebkaifadetongzhikenengpengdaoguoxuyaozaiyemianliqianrufasongyoujiandegongnengruguoshiputongdechunwenbendeyoujianhuanhaomeiwentiyongaspyouhaoduozujianyongcgiyeyouhaogongjubiruperlzaiperlzhong使shiyongunixpingtaixiadesendmailkeyishixianzhegemudePerlzhongfasongchunwenbenyoujiandedianxingliziruxia

  #!/usr/lib/perl

  use strict;

  my($r_mail) = 'recipients@aaa.net';

  my($s_mail) = 'sender@bbb.com';

  my($subject) = 'subject';

  open(MAIL,'|/usr/lib/sendmail -t');

  select(MAIL);

  print<<"END_TAG";

  To: $r_mail

  From: $s_mail

  Subject: $subject

  youjianneirong

  END_TAG

  youjidianyaozhuyizaifasongyoujianliTo, Fromhejieshouzheyoujiandizhibianliang$r_mailyijifasongzheyoujian$s_mailzhijianyaoliuyigekonggebimianbubiyaodebaocuowentiwoyudaoguobuzhidaoniyoumeiyoupengdaohuanyounagejieshubiaojiEND_TAGruguoshiwenjiandezuihouyixingzuihaozaihoumianjiayilianggekongxingwozengjingpengdaomeihoumiandekongxingperlzhaobudaoEND_TAGdeqingkuanghuanyoubuyaowanglesubjectzhihoudenagekongxingshibixudetafenkaileyoujiantouheyoujianneirong

  haojinruzhengtiruguowomenxuyaofasonghtmlgeshideyoujianneruguoxiechengzheyang

  #!/usr/lib/perl

  use strict;

  my($r_mail) = 'recipients@aaa.net';

  my($s_mail) = 'sender@bbb.com';

  my($subject) = 'subject';

  open(MAIL,'|/usr/lib/sendmail -t');

  select(MAIL);

  print<<"END_TAG";

  To: $r_mail

  From: $s_mail

  Subject: $subject

  <html<body<a href=#youjianneirong</a</body</html

  END_TAG

  shishikanzai263liyuandaimaquanxianshichulailezaihotmailzhonghaodianruguoyoujianshigewanzhengdehtmlyoujianjibenshangnenggouwanzhengdechengxianhtmlyemianqishizhelitouyougeMIMEleixingdewentixiangxideMIMEziliaodajiazijishangwangzhaobafouzechedetaiyuanwozhedianshuibugoudaoderuguozhegehtmlyoujianmeiyoulianjierenhetupianyijicileidewaibuneirongnahaobanzaiyoujiantoubufenjiayijuContent-type:text/htmljiukeyileruguo使shiyonglezhongwenxuyaozhidingyixiadaimayezhijiezaihoumianzaitianshangcharset="gb2312",zhongjianyongfenhaogekaiwanzhengdaimaruxia

  #!/usr/lib/perl

  use strict;

  my($r_mail) = 'recipients@aaa.net';

  my($s_mail) = 'sender@bbb.com';

  my($subject) = 'subject';

  open(MAIL,'|/usr/lib/sendmail -t');

  select(MAIL);

  print<<"END_TAG";

  To: $r_mail

  From: $s_mail

  Subject: $subject

  Content-type:text/html;charset="gb2312"

  <html<body<a href=#youjianneirong</a</body</html

  END_TAG

  zheyangyiban使shiyongdejieshouyoujiandegongjudounengkandaohtmlgeshideyoujianleruguowentizaifuduoyidianzhegehtmlyemianliyoutuhuanyouflashnazenmebanhuiyoubanfabazhexietupianfangzaiwangshangyemiandetupiandouxiequanlujinglianjiezheyangjiugenbenbuxuyaozaiyoujianlizhendedaishangzhexieleizhuilebingqiehuanjianxiaoleyoujiandedaxiaoyijuliangdewoyanchongzantongdanshizongyoupengdaobunengzheyanggandeshihousuoyijixuhtmlyemiandeduizhexietudelianjiebingbunenggourangyonghushoudaodeyoujianliyouzhexietuheflashwenjiankandaodehtmlyemianshikailetianchuangdeyemiankankanMIMEleixingyougemultipart/mixeddeleixingnenggouwanchengwomendezuizhongmuderangyonghushoudaodeyoujianshituwenbingjudewanzhengyemianshouxianxuyaoanyidingdebianmafangfaduitupianhuozheflashdengwenjianbianmadianziyoujianzhongzuichangyongdeshibase64bianmahuanyouquoted-printablebianmazhaogegongjubatupiandengxuyaolianruhmtlyoujiandewenjian使shiyongbase64bianmaduihtmlyoujianze使shiyongquoted-printablebianmaranhouzaiyoujiantouxieContent-Type: multipart/mixed;boundary="----=_NextPart_000_0008_01C2BCB0.9CF9AE70" name="thanks.gif"zhelidemultipart/mixedbiaoshibenyoujianshihunheleixingdeyoujianjiexialaideboundaryshizhidingfengeyoujianneirongligebutonggebufendebiaojishishenmezhelijiushi----=_NextPart_000_0008_01C2BCB0.9CF9AE70lezhegezhibixuyaozenyangwobushihenqingchuwodelijieshizaibenyoujianzhongnenggoubuyurenhebianmahoudemouduanneirongxiangtongjiukeyilehoumiannagenamekeyibuyaoshuoqilaibijiaoluosuohuanshixiankandaimaba

  xiamianjiushigewanzhengdefasonghmtlyoujiandelizi

  #!/usr/lib/perl

  use strict;

  my($r_mail) = 'recipients@aaa.net';

  my($s_mail) = 'sender@bbb.com';

  my($subject) = 'subject';

  open(MAIL,'|/usr/lib/sendmail -t');

  select(MAIL);

  print<<"END_TAG";

  To: $r_mail

  From: $s_mail

  Subject: $subject

  Content-Type: multipart/mixed;boundary="----=_NextPart_000_0008_01C2BCB0.9CF9AE70"

  This is a multi-part message in MIME format.

  ------=_NextPart_000_0008_01C2BCB0.9CF9AE70

  Content-Type: text/html;charset="gb2312"

  Content-Transfer-Encoding: quoted-printable

  <HTML<HEAD<TITLE=D0=BB=D0=BB=C4=FA=B5=C4=B2=CE=D3=EB=A3=A1</TITLE

  <META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312"

  <META content=3D"MSHTML 6.00.2800.1126" name=3DGENERATOR</HEAD

  <BODY text=3D#000000 bgColor=3D#ffffff leftMargin=3D0 topMargin=3D0<IMG =

  height=3D400=20

  src=3D"file:///C:/DEV/perl/images/popup_thanks.gif" =

  width=3D400=20

  border=3D0</BODY</HTML

  ------=_NextPart_000_0008_01C2BCB0.9CF9AE70

  Content-Type: image/gif

  Content-Transfer-Encoding: base64

  Content-Disposition: attachment;filename="thanks.gif"

  Content-Location: file:///C:/DEV/perl/images/popup_thanks.gif

  R0lGODlhkAGQAfcAAPCyTdvr0cvbCJ8tTt3nAEeRGarPkRSmULrVqrRtLtbeB7CRNdTZ0LGtNaXJ

  EaLR7ejz23e1GFir2NNMayaR0VWaLfjMRtnINsfWuYi6aGWsNevKN5PHdLZKMyZLC6smNs3nu8mI

  NWetGBIqCOqXNdutkWyRRzZvEqvIK9fJSZGMNrSxSlalGO/36rS5revt6MySScswSperNZWtT9C3

  ------=_NextPart_000_0008_01C2BCB0.9CF9AE70--

  END_TAG

  youdianchanglemanmanjieshibazhefengyaofasongdehmtlyoujianlizhiyouyizhangtupianpopup_thanks.gif. limianyouyijuhua"This is a multi-part message in MIME format.", fangzaidiyigeboundarychuxianzhiqianzheshigemiaoshuxinxibuyongguantaranhoujiushidiyigeboundary:------=_NextPart_000_0008_01C2BCB0.9CF9AE70,tagaosuyonghudeyoujianchengxuzheliyouyibufendeneirongzhuyizhelishi--boundary,jiushishuozaiboundaryqianmianjialeliangge-,dajiahuanqingzhuyikanzuihouyigeboundary,tadeqianhoudoujialeliangge-biaoshizhenggeyoujianjieshu

  Content-type:text/html;charset="gb2312" shuomingbenbufenneirongdewendangleixingshihtmlgeshideContent-Transfer-Encoding: quoted-printable shuomingbenbufenneirong使shiyong quoted-printable fangfabianmadedangranxiamiandeneirongyaoqueshishi quoted-printable bianmadefouzeyonghujiukanbudaozhengquedeneirongle

  youjianneirongmeishenmehaoshuoderanhoushixiayige boundaryzhelidedong西xijiushiwomenyaodenagepopup_thanks.gifle

  kanMIMEleixingshiContent-Type: image/gif tupianyibanjiuyongbase64bianmasuoyizhelishiContent-Transfer-Encoding: base64 zaikanxiamianshiyixingContent-Disposition: attachment;filename="thanks.gif" zhelideattahmentbiaoshicitupianzuoweifujiantahuankeyishiinlinenayangdehuazhegetupianjiuhuizhijiezaishoujianrendeyoujianchengxudeyoujianxianshiquyulixianshilefilenamezhidinglezaifujianquyuxianshishenmeyangdewenjianmingzhelijiubapopup_thanks.gifgaichenglethanks.gif.xiamianhuanyouyijuContent-Location: file:///C:/DEV/perl/images/popup_thanks.gifzhidingwenjiandeyuanshilujinghaoxiangmeiyongaqishihenchongyaozhuyihtmlwenjianlilianjiezhegetupiandebiaoqianlidesrcshizenmeyangxiedezhelianggezhijianyaoshiduibushangnamoyoujianxianshideshihoufujianliyoutudanyoujianhuanshikailetianchuanglehaolejibenjiushizheyangbuhuanyougewentizuochengxudeshihouzenyangcainengdedaoxuyaodebianmahoudewenjianaperllizenmeyangzuowobuzhidaoCPANliyexuyouzheyangdepackagebanaweiduibianmashuxiyekeyizijixiebuguowozuodeshihouquqiaole

  dajiayongguoIE5delingcunwei.mhtwenjianmaduilejiushitabaxuyaofasongdehtmlyoujianyongIE5zaibendidakaizailingcunweimhtwenjiansuoyoudebianmadoudedaoleerqietupiandelianjieguanxiyedoushixianchengdeleqitadeanxuyaotiaozhengyixia,tiedaonidechengxulijiuwanshidajigengjinyiburuguoxuyaozuodaoxiang263nayangcongyemianshangjieshoubaokuozhengwenjieshouzheyijigezhongkenengdefujiandengxinxizaifasongne

  原文
 
 
  搞web开发的同志可能碰到过需要在页面里嵌入发送邮件的功能,如果是普通的纯文本的邮件还好,没问题,用asp有好多组件,用cgi也有好工具,比如perl。在perl中使用unix平台下的sendmail可以实现这个目的。Perl中发送纯文本邮件的典型例子如下:   #!/usr/lib/perl   use strict;   my($r_mail) = 'recipients@aaa.net';   my($s_mail) = 'sender@bbb.com';   my($subject) = 'subject';   open(MAIL,'|/usr/lib/sendmail -t');   select(MAIL);   print<<"END_TAG";   To: $r_mail   From: $s_mail   Subject: $subject   邮件内容   END_TAG   有几点要注意,在发送邮件里To, From和接受者邮件地址变量$r_mail以及发送者邮件$s_mail之间要留一个空格,避免不必要的报错问题(我遇到过,不知道你有没有碰到)。还有那个结束标记END_TAG如果是文件的最后一行,最好在后面加一两个空行,我曾经碰到没后面的空行perl找不到END_TAG的情况。还有,不要忘了subject之后的那个空行是必须的,它分开了邮件头和邮件内容。   好,进入正题,如果我们需要发送html格式的邮件呢?如果写成这样   #!/usr/lib/perl   use strict;   my($r_mail) = 'recipients@aaa.net';   my($s_mail) = 'sender@bbb.com';   my($subject) = 'subject';   open(MAIL,'|/usr/lib/sendmail -t');   select(MAIL);   print<<"END_TAG";   To: $r_mail   From: $s_mail   Subject: $subject   <html<body<a href=#邮件内容</a</body</html   END_TAG   试试看,在263里源代码全显示出来了,在hotmail中好点,如果邮件是个完整的html邮件,基本上能够完整的呈现html页面。其实这里头有个MIME类型的问题。详细的MIME资料大家自己上网找吧,否则扯得太远,我这点水不够倒的。如果这个html邮件没有连接任何图片以及此类的外部内容,那好办,在邮件头部分加一句Content-type:text/html就可以了。如果使用了中文需要指定一下代码页,直接在后面在添上charset="gb2312",中间用分号格开。完整代码如下:   #!/usr/lib/perl   use strict;   my($r_mail) = 'recipients@aaa.net';   my($s_mail) = 'sender@bbb.com';   my($subject) = 'subject';   open(MAIL,'|/usr/lib/sendmail -t');   select(MAIL);   print<<"END_TAG";   To: $r_mail   From: $s_mail   Subject: $subject   Content-type:text/html;charset="gb2312"   <html<body<a href=#邮件内容</a</body</html   END_TAG   这样一般使用的接收邮件的工具都能看到html格式的邮件了。如果问题再复杂一点,这个html页面里有图,还有flash,那怎么办?会有办法:把这些图片放在网上,页面的图片都写全路径链接,这样就根本不需要在邮件里真的带上这些累赘了,并且还减小了邮件的大小,一举两得!我严重赞同。但是总有碰到不能这样干的时候,所以继续。html页面的对这些图的链接并不能够让用户收到的邮件里有这些图和flash文件。看到的html页面是开了天窗的页面。看看MIME类型,有个multipart/mixed的类型能够完成我们的最终目的,让用户收到的邮件是图文并举的完整页面。首先需要按一定的编码方法对图片或者flash等文件编码,电子邮件中最常用的是base64编码,还有quoted-printable编码。找个工具,把图片等需要链入hmtl邮件的文件使用base64编码,对html邮件则使用quoted-printable编码。然后,在邮件头写Content-Type: multipart/mixed;boundary="----=_NextPart_000_0008_01C2BCB0.9CF9AE70" name="thanks.gif"这里的multipart/mixed表示本邮件是混合类型的邮件。接下来的boundary是指定分隔邮件内容里各不同各部分的标记是什么。这里就是----=_NextPart_000_0008_01C2BCB0.9CF9AE70了。这个值必须要怎样我不是很清楚,我的理解是在本邮件中能够不与任何编码后的某段内容相同就可以了。后面那个name可以不要。说起来比较罗索,还是先看代码吧。   下面就是个完整的发送hmtl邮件的例子。   #!/usr/lib/perl   use strict;   my($r_mail) = 'recipients@aaa.net';   my($s_mail) = 'sender@bbb.com';   my($subject) = 'subject';   open(MAIL,'|/usr/lib/sendmail -t');   select(MAIL);   print<<"END_TAG";   To: $r_mail   From: $s_mail   Subject: $subject   Content-Type: multipart/mixed;boundary="----=_NextPart_000_0008_01C2BCB0.9CF9AE70"   This is a multi-part message in MIME format.   ------=_NextPart_000_0008_01C2BCB0.9CF9AE70   Content-Type: text/html;charset="gb2312"   Content-Transfer-Encoding: quoted-printable   <HTML<HEAD<TITLE=D0=BB=D0=BB=C4=FA=B5=C4=B2=CE=D3=EB=A3=A1</TITLE   <META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312"   <META content=3D"MSHTML 6.00.2800.1126" name=3DGENERATOR</HEAD   <BODY text=3D#000000 bgColor=3D#ffffff leftMargin=3D0 topMargin=3D0<IMG =   height=3D400=20   src=3D"file:///C:/DEV/perl/images/popup_thanks.gif" =   width=3D400=20   border=3D0</BODY</HTML   ------=_NextPart_000_0008_01C2BCB0.9CF9AE70   Content-Type: image/gif   Content-Transfer-Encoding: base64   Content-Disposition: attachment;filename="thanks.gif"   Content-Location: file:///C:/DEV/perl/images/popup_thanks.gif   R0lGODlhkAGQAfcAAPCyTdvr0cvbCJ8tTt3nAEeRGarPkRSmULrVqrRtLtbeB7CRNdTZ0LGtNaXJ   EaLR7ejz23e1GFir2NNMayaR0VWaLfjMRtnINsfWuYi6aGWsNevKN5PHdLZKMyZLC6smNs3nu8mI   NWetGBIqCOqXNdutkWyRRzZvEqvIK9fJSZGMNrSxSlalGO/36rS5revt6MySScswSperNZWtT9C3   ------=_NextPart_000_0008_01C2BCB0.9CF9AE70--   END_TAG   有点长了,慢慢解释吧。这封要发送的hmtl邮件里只有一张图片popup_thanks.gif. 里面有一句话"This is a multi-part message in MIME format.", 放在第一个boundary出现之前,这是个描述信息,不用管它。然后就是第一个boundary:------=_NextPart_000_0008_01C2BCB0.9CF9AE70,它告诉用户的邮件程序这里有一部份的内容。注意这里是--boundary,就是说在boundary前面加了两个-,大家还请注意看最后一个boundary,它的前后都加了两个-,表示整个邮件结束。   Content-type:text/html;charset="gb2312" 说明本部分内容的文档类型是html格式的,Content-Transfer-Encoding: quoted-printable 说明本部分内容使用 quoted-printable 方法编码的,当然,下面的内容要确实是 quoted-printable 编码的,否则用户就看不到正确的内容了。   邮件内容没什么好说的,然后是下一个 boundary,这里的东西就是我们要的那个popup_thanks.gif了。   看MIME类型是:Content-Type: image/gif 图片一般就用base64编码,所以这里是Content-Transfer-Encoding: base64 再看下面是一行Content-Disposition: attachment;filename="thanks.gif" 这里的attahment表示此图片作为附件,它还可以是inline,那样的话这个图片就会直接在收件人的邮件程序的邮件显示区域里显示了。filename指定了在附件区域显示什么样的文件名,这里就把popup_thanks.gif改成了thanks.gif.下面还有一句Content-Location: file:///C:/DEV/perl/images/popup_thanks.gif指定文件的原始路径。好像没用啊?其实很重要,注意html文件里连接这个图片的标签里的src是怎么样写的?这两个之间要是对不上,那末邮件显示的时候,附件里有图,但邮件还是开了天窗了。好了,基本就是这样。不,还有个问题,做程序的时候,怎样才能得到需要的编码后的文件啊?perl里怎么样做我不知道,CPAN里也许有这样的package吧,那位对编码熟悉,也可以自己写,不过我做得时候取巧了。   大家用过IE5的另存为.mht文件吗?对了,就是它!把需要发送的html邮件用IE5在本地打开,再另存为mht文件,所有的编码都得到了,而且图片的链接关系也都是现成的了,其他的按需要调整一下,帖到你的程序里就万事大吉。更进一步,如果需要做到像263那样,从页面上接收包括正文,接受者以及各种可能的附件等信息再发送呢?   
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

网络安全治理:国家安全保障的主要方向是打击犯罪,而不是处置和惩罚受害者

来源:中国青年报 新的攻击方法不断涌现,黑客几乎永远占据网络攻击的上风,我们不可能通过技术手段杜绝网络攻击。国家安全保障的主要方向是打击犯罪,而不是处置和惩罚...

 
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
黑色魅力_醉人的美
梦幻蒙眬_如痴如醉
北京的“尖叫”建筑
高天上流云
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有