为什么发送的电子邮件没有主题?

时间:2018-11-22 17:56:33

标签: python email

主题不随邮件一起发送,这是我第一次使用python该怎么办? 我需要添加更多附件吗? 有人可以找出这段代码有什么问题吗?我认为mailServer无法正常工作。

#!/usr/bin/python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "mail@jj.com"
gmail_pwd = "password"

def mail(to, subject, text):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   mailServer.close()


f = open("try1.txt","r")
for line in f:
    mail(line, "test", "bla bla bla bla balb")

0 个答案:

没有答案