Python 将文本从.txt文件发送到电子邮件
有没有办法将文本文件(.txt)中的文本发送到电子邮件。我成功地将附件文本文件发送到电子邮件,但我只想发送文本,而不是整个文件。从.txt文件中检索文本并发送它们 这是我发送电子邮件附件的代码Python 将文本从.txt文件发送到电子邮件,python,email,Python,Email,有没有办法将文本文件(.txt)中的文本发送到电子邮件。我成功地将附件文本文件发送到电子邮件,但我只想发送文本,而不是整个文件。从.txt文件中检索文本并发送它们 这是我发送电子邮件附件的代码 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
email = 'assassin@gmail.com' # Your email
password = '123abc' # Your email account password
send_to_email = 'james12@gmail.com' # Who you are sending the message to
subject = 'subject' # The subject line
message = 'ok' # The message in the email
file_location = r'C:\Users\hp\Downloads\SimpleCoin-master\SimpleCoin-master\simpleCoin\output.txt'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
# Attach the message to the MIMEMultipart object
msg.attach(MIMEText(message, 'plain'))
# Setup the attachment
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# Attach the attachment to the MIMEMultipart object
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string() # You now need to convert the MIMEMultipart object to a string to send
server.sendmail(email, send_to_email, text)
server.quit()
我只想发送电子邮件正文中的文本,而不是整个文件附件。您可以使用以下代码执行此操作:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
email = 'assassin@gmail.com' # Your email
password = '123abc' # Your email account password
send_to_email = 'james12@gmail.com' # Who you are sending the message to
subject = 'subject' # The subject line
message = 'ok' # The message in the email
file_location = r'C:\Users\hp\Downloads\SimpleCoin-master\SimpleCoin-master\simpleCoin\output.txt'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
# Attach the message to the MIMEMultipart object
msg.attach(MIMEText(message, 'plain'))
# Setup the attachment
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# Attach the attachment to the MIMEMultipart object
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string() # You now need to convert the MIMEMultipart object to a string to send
server.sendmail(email, send_to_email, text)
server.quit()
导入smtplib
导入操作系统路径
电子邮件assassin@gmail.com“#您的电子邮件
密码='123abc'#您的电子邮件帐户密码
发送电子邮件至james12@gmail.com“#你要向谁发送信息
主题='subject'#主题行
文件位置=#此处的文件位置
打开(文件位置)为f时:
message=f.read()
server=smtplib.SMTP('SMTP.gmail.com',587)
server.starttls()
服务器登录(电子邮件、密码)
text=“主题:{}\n\n{}”。格式(主题,消息)
server.sendmail(电子邮件、发送电子邮件、文本)
server.quit()
您已经尝试了什么?你面临哪些具体问题?感谢您的考虑和支持。我尝试发送附件文件,并按照我的预期成功发送,但现在我只想发送文本。请共享您的codeShared@ChristianBaumann。除了发送整个文件,您可以导入文件并将文本添加到正文中。