文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>脚本发邮件的几种方法

脚本发邮件的几种方法

时间:2009-05-02  来源:linewer

(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='[email protected]';
$TO='[email protected]';
$subject='From Perl Module';
$SMTPUSER='[email protected]';
$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','[email protected]')


(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是关于域名解析的问题,记不清了~~
相关阅读 更多 +
排行榜 更多 +
捕鱼大咖官方正版下载

捕鱼大咖官方正版下载

角色扮演 下载
方舟生存进化2.0手机版下载安装

方舟生存进化2.0手机版下载安装

角色扮演 下载
图书馆挠痒大作战游戏下载

图书馆挠痒大作战游戏下载

休闲益智 下载