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是关于域名解析的问题,记不清了~~
|