Chinaunix首页 | 论坛 | 博客
  • 博客访问: 484860
  • 博文数量: 279
  • 博客积分: 4467
  • 博客等级: 上校
  • 技术积分: 2830
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-03 14:43
文章分类

全部博文(279)

文章存档

2013年(1)

2012年(39)

2011年(35)

2009年(29)

2008年(131)

2007年(44)

分类:

2009-05-04 15:47:19

1)mail和sendmail就不说了,man一下
(2)Perl调用sendmail:

#mysendmail.pl



open (SENDMAIL, "|/usr/sbin/sendmail -oi -f linewer\@localhost.localdomain -t") or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: <linewer\@localhost.localdomain>
To: <gx2008758\@gmail.com>
Subject: "MailFrom perl"
"just test it"
EOF
close (SENDMAIL) or warn "sendmail did not close nicely";
(3).perl模块:

use MIME::Lite; #这个用来组织邮件信息,发信的是用下面模块!

use Net::SMTP;
use Net::SMTP::SSL;
$FROM='gx2008758@sohu.com';
$TO='gx2008758@gmail.com';
$subject='From Perl Module';
$SMTPUSER='gx2008758@sohu.com';
$SMTPPASS='youguessit';
$message_text='testing again';
 
my $msg = MIME::Lite->new(From => $FROM,To => $TO,Subject => $subject,Type => 'multipart/alternative');
$msg->attach(Encoding =>'quoted-printable',Type =>'text/plain;charset=UTF-8',Data => $message_text);
$msg->attach(Encoding =>'base64',Type =>'image/jpeg',Filename => '1.jpg',Path => '../1.jpg', Disposition => 'attachment');
#for no starttls mail server:sohu


my $smtp = Net::SMTP->new('smtp.sohu.com',Timeout => 60, Debug => 1) or die "new error\n";
#for starttls mail server:gmail

#my $smtp=Net::SMTP::SSL->new('smtp.gmail.com',Port=>465,Timeout => 60, Debug => 1) or die "new error\n";


$smtp->auth($SMTPUSER, $SMTPPASS);
$smtp->mail($FROM);
$smtp->to($TO);
$smtp->data($msg);
$smtp->quit;

(4).python调用sendmail:
import os

def email(subject,content,sender='localhost',receiver='me'):
    mail_body="""From:%s
To:%s
Mime-Version: 1.0
Content-Type: text/html;charset=utf8
Subject:%s
%s"
"" % (sender,receiver,subject,content)
    cmd='echo "%s"|/usr/sbin/sendmail -t' % (mail_body)
    os.system(cmd)
if __name__=="__main__" :
   email('Hi,From Python','Just test it','gg','gx2008758@gmail.com')


(5).python模块:
def send_mail(send_from, send_to, subject, text,auth=(), send_server='localhost'):
    msg = email.MIMEMultipart.MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = email.Utils.COMMASPACE.join(send_to)
    msg['Date'] = email.Utils.formatdate(localtime=True)
    msg['Subject'] = email.Header.Header(subject,'utf-8')
    msg.attach(email.MIMEText.MIMEText(text,_subtype='plain',_charset='utf-8'))
    
    smtp = smtplib.SMTP(send_server)
    #the two lines below are neccessary to

    smtp.docmd("EHLO server" )
    smtp.starttls()
    smtp.login(*auth)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()

ps:sendmail发现有时会有延迟,这个要改下senmail设置,MS是关于域名解析的问题,记不清了~~

阅读(1011) | 评论(0) | 转发(0) |
0

上一篇:Debian编译内核

下一篇:History

给主人留下些什么吧!~~