我以前试过,但很管用,不知道会发生什么..首先,它应该读取一个文本文件。我试着手动阅读和打印它,它工作了,所以这部分是正常的。我认为。但每次我尝试发送消息时,都会收到一条空消息
import smtplib as s
#file settings
filename = open("D:\pythonscripts\hey_hey.txt", 'a+')
content = filename.read()
#recivers list
recivers = ['somemail@gmail.com']
#tries to send the message
def send_mail(username, password, msg):
    #connects to smtp gmail server
    server = s.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username, password)
    try:
        server.sendmail(username, recivers[0], msg)
        print("massage sent")
    except:
        server.close()
        print("error while sending the massage\nquitung server...")
send_mail('somemail@gmail.com', 'ferari_car', content)发布于 2017-06-30 23:20:08
试着这样做:
filename = open("D:\\pythonscripts\\hey_hey.txt", 'r')  # r, not a+, and escape the backslashesa+打开要追加的文件,这会将文件位置放在文件的末尾。所以当你试着读它的时候,你什么也得不到。
发布于 2017-07-01 19:30:33
嘿,大家好,谢谢你们帮我。很显然,我设法将邮件与文本文件一起作为附件发送,而不是邮件正文。
import smtplib as s
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
#file settings
filename = open("D:\\pythonscripts\\hey_hey.txt", 'r')
content= filename.read()
print(content)
#recivers list
recivers = ['somemail@gmail.com']
#tries to send the message
def send_mail(username, password, message, send_to, subject):
    #message settings
    msg = MIMEMultipart()
    msg['From'] = username
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
    msg.attach(MIMEText(message))
    #connects to smtp gmail server
    server = s.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username, password)
    try:
        server.sendmail(username, send_to, msg.as_string())
        print("massage sent")
    except:
        server.close()
        print("error while sending the massage\nquitung server...")
send_mail('othermail@gmail.com', 'ferari_car', content, recivers[0], "the mail stuff")https://stackoverflow.com/questions/44849778
复制相似问题