python 发送tsl加密邮件


python版本测试通过。但是我在用expect实现,在tsl加密之后,不知道该怎么处理了。

两个都帖进来。

#!/usr/bin/env python
#-*-coding = UTF-8-*-

import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import sys,socket

toaddr = "xxx@xxx"

msg = MIMEMultipart()
msg['From'] = "no-reply@xxx.com"
msg['To'] = toaddr
msg['Subject'] = 'this is letter subject'

fileName = r'test.jpg'
ctype, encoding = mimetypes.guess_type(fileName)
if ctype is None or encoding is not None:
    ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
att1 = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)
att1.add_header('Content-Disposition', 'attachment', filename = fileName)
msg.attach(att1) 

s = smtplib.SMTP("xxx.xxx.com",xxx)
try:
    code = s.ehlo()[0]
    if code != 250:
        usesesntp = 0
        code = s.helo()[0]
        if not code != 250:
            raise SMTPHeloError(code,resp)
    if s.has_extn('starttls'):
        code = s.starttls()
        code = s.ehlo()[0]
        if code != 250:
            print "Couldn't EHLO after STARTTLS."
            sys.exit(1)
    else:
        print "Server does not suport TLS; using normal connection."
        sys.exit(1)

    if s.has_extn('auth'):
        try:
            s.login('xxx','xxx')
        except smtplib.SMTPException,e:
            print "Authentication failed:",e
            sys.exit(1)
    else:
        print "Server does not suport Authentication; using normal connect."
        sys.exit(1)

    #if usesesmtp and s.has_extn('size'):
        #print "Maxinum message size is ",s.esmtp_features['size']

    s.sendmail("no-reply@xxx.com",toaddr,message)

except(socket.gaierror,socket.error,socket.herror,smtplib.SMTPException),e:
    print "***Your message may not have been sent!"
    print e
    sys.exit(1)
else:
    print "***Message successful sent to %d recipient(s)" % len(toaddr)

下面的是expect版本的


#!/usr/bin/expect
set smtp   xxx.xxx.xxx
set user   xxxxx
set pass   xxxxx
set from   no-reply@xxx.com
set to     xxx
set title  testtitle!
set content testcontent!
spawn telnet $smtp 587
expect "221"
send "HELO LOCALHOST\r"
expect "250"
send "STARTTLS\r"
expect "220"
send "HELO LOCALHOST\r"
expect "250"
send "AUTH LOGIN\r"
expect "334"
send "$user\r"
expect "334"
send "$pass\r"
expect "235"
send "MAIL FROM: <$from>\r"
expect "250"
send "RCPT TO: <$to>\r"
expect "250"
send "DATA\r"
expect "354"
send "TO: $to\r"
send "FROM: $from\r"
send "SUBJECT: $title\r"
send "Content-Type: text/html; charset=\"UTF-8\"\r"
send "\r"
send "$content\r"
send ".\r"
expect "250"
send "QUIT"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值