关于python2.6.5下的 smtplib 问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于python2.6.5下的 smtplib 问题相关的知识,希望对你有一定的参考价值。

用python 的 smtplib 做例子,总是出错,以下为出错

File "email.py", line 1, in <module>
import smtplib
File "/usr/local/lib/python2.6/smtplib.py", line 46, in <module>
import email.utils
File "/home/pythonlib/email.py", line 2, in <module>
from email.mime.text import MIMEText
ImportError: No module named mime.text

以下为代码:

import smtplib
from email.mime.text import MIMEText

fromaddr = 'chang@gmail.com'
toaddrs = 'chang@qq.com'
msg = MIMEText('hello world')
msg['Subject'] = 'I miss you'
msg['From'] = 'chang@gmail.com'
msg['To'] = 'chang@qq.com'

# Credentials (if needed)
username = 'chang'
password = '××××××××'

# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()

实在看不出错误,是PYTHON版本的问题? 我的运行环境是 centos5.5 python2.6.5

File "email.py", line 1, in <module>
import smtplib
File "/usr/local/lib/python2.6/smtplib.py", line 46, in <module>
import email.utils
File "/home/pythonlib/email.py", line 2, in <module>
from email.mime.text import MIMEText
ImportError: No module named mime.text

分析下错误:email.py第一行报错,楼主这个文件是email.py吧。后面又有smtplib.py中import email.utils这一句,又说email.py中无mime.text。可以推断是文件名与标准库名冲突。把文件名改成gmail.py就可以了。
参考技术A 你的文件名与标准库中的文件名冲突了,import的时候会自动优先搜索你的文件所在目录,所以就从你的这个程序去引用了,因此找不到module,改个名字就好了。

python smtplib发送邮件

python 发送邮件

smtplib模块负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。

email模块负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。

email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。

该mime包下常用的有三个模块:text,image,multpart

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

#邮件服务器信息
smtp_server = "smtp.qq.com"
port = 465  # For starttls
sender_email = "12345689@qq.com"
password=""  #get password from mailsetting

#发送邮件信息,可以发送给多个收件人
receivers=["12345689@163.com","12345689@qq.com"]
subject="This is import Python SMTP 邮件(文件传输) 多媒体测试"

# message = MIMEText(text, "plain", "utf-8") #文本邮件
message = MIMEMultipart()
message["Subject"] = Header(subject, "utf-8")
message["from"] = sender_email
message["to"] = ",".join(receivers)
# 邮件正文内容
text="""
Dear Sir:
how are you ? 

for detail information pls refer to attach1。

The files you need are as followed.

If you have any concern pls let me known.

enjoy your weekend.

BEST REGARDS 

"""
# message.attach(MIMEText(‘for detail information pls refer to attach1。
 The files you need are as followed. 
 If you have any concern pls let me known. 
 enjoy your weekend‘, ‘plain‘, ‘utf-8‘)
message.attach(MIMEText(text,plain,utf-8))

# 构造附件1
attach_file1=IMG1965.JPG

attach1 = MIMEText(open(attach_file1, rb).read(), base64, utf-8)
attach1["Content-Type"] = application/octet-stream
attach1["Content-Disposition"] = attachment; filename={0}.format(attach_file1)
message.attach(attach1)

# 构造附件2
attach_file2=YLJ.jpg
attach2 = MIMEText(open(attach_file2, rb).read(), base64, utf-8)
attach2["Content-Type"] = application/octet-stream
attach2["Content-Disposition"] = attachment; filename={0}.format(attach_file2)
message.attach(attach2)

# Try to log in to server and send email
# server = smtplib.SMTP_SSL(smtp_server,port)
server = smtplib.SMTP_SSL(smtp_server,port)

try:
    server.login(sender_email, password)
    server.sendmail(sender_email,receivers,message.as_string())
    print("邮件发送成功!!!")
    print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers))
except Exception as e:
    # Print any error messages to stdout
    print("Error: 无法发送邮件")
    print(e)
finally:
    server.quit()

结果

邮件发送成功!!!
Mail with IMG1965.JPG & IMG1963.jpg has been send to [‘12345689@163.com‘, ‘12345689@qq.com‘] successfully.

以上是关于关于python2.6.5下的 smtplib 问题的主要内容,如果未能解决你的问题,请参考以下文章

Django 环境搭建

如何将unicode字符串写入文件? [复制]

使用 Python 示例发送电子邮件失败

Nginx+uwsgi+virtualenv+Django+Mysql架构

在 Python 2.6.5 中,我可以为 urllib.quote 和 urllib.unquote 使用 unicode-ready 替代品吗?

关于在 App Store 中将 APP 下的公司名称改成英文或者中文的相关流程