通过Python发送带有附件的邮件

问题描述 投票:1回答:1
import smtplib
import mechanize
import os
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.base import MIMEBase 
from email import encoders 

# =============================================================================
# SET EMAIL LOGIN REQUIREMENTS
# =============================================================================
if not os.path.isfile('key.txt'):
    print('below details are req to send report')
    gmail_user = input('enter your email=')
    gmail_app_password = input('enter your email password=')
    print('pls accept the login in your gmail account ')
    ke = open('key.txt',mode="w+") 
    ke.write(gmail_user)
    ke.write(':')
    ke.write(gmail_app_password)
    ke.close()
if not os.path.isfile('sto.txt'):
    gmai = input('enter the  email to send report=')
    ke = open('sto.txt',mode="w+") 
    ke.write(gmai)
    ke.close()


with open('key.txt',mode="r")as f:
    ds=f.readlines()
    d=''.join(ds)
    r=d.split(':')
with open('sto.txt',mode="r")as f:
    ds=f.readlines()


f=ds
print(f)
gmail_user = r[0]
gmail_app_password = r[1]



# =============================================================================
# SET THE INFO ABOUT THE SAID EMAIL
# =============================================================================
sent_from = gmail_user
sent_to = dsg
sent_subject = "hey amo lio ,how are ?"
sent_body = ("Hey, what's up? friend!\n\n"
             "I hope you have been well!\n"
             "\n"
             "Cheers,\n"
             "Jay\n")

email_text = """\
To: %s
Subject: %s

%s
""" % (", ".join(sent_to), sent_subject, sent_body)

# =============================================================================
# SEND EMAIL OR DIE TRYING!!!
# Details: http://www.samlogic.net/articles/smtp-commands-reference.htm
# =============================================================================

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_app_password)
    server.sendmail(sent_from, sent_to, email_text)
    server.close()

    print('Email sent!')
except Exception as exception:
    print("Error: %s!\n\n" % exception)

如何在此电子邮件中附加helloword.txt文件?这段代码运行良好,我只想随它一起发送附件。这段代码可以让我发送没有任何附件的尸体。另外,如何加密存储电子邮件地址和密码的key.txt文件,并发送电子邮件要求输入密码(diff pass)?

python email mime
1个回答
3
投票
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText mail = MIMEMultipart() mail["Subject"] = sent_subject mail["From"] = sent_from mail["To"] = sent_to mail.attach[MIMEText(sent_body,'html')] ctype, encoding = mimetypes.guess_type(_file) maintype, subtype = ctype.split('/', 1) fp = open("/path/to/attachment/file.txt") # If file mimetype is video/audio use respective email.mime module. # Here assuming 'maintype' == 'text' we will use MIMEText msg = MIMEText(fp.read(), _subtype=subtype) fp.close() filename = os.path.basename(_file) msg.add_header('Content-Disposition', 'attachment', filename=filename) mail.attach(msg) server.sendmail(sent_from, sent_to, mail.as_string())
© www.soinside.com 2019 - 2024. All rights reserved.