我们可以使用pyautoit模块将文件附加到gmail吗?

时间:2017-01-31 19:18:03

标签: python selenium automation gmail attachment

{{1}}

我能够登录gmail。我能够撰写邮件,但我无法附加任何文件。任何人都可以建议我附加文件的方法吗?这是可能的硒还是我必须使用pyautoit模块?

1 个答案:

答案 0 :(得分:1)

通过努力避免“正确”的做事方式,你在这里走的路很艰难。用硒放下目前的方法,并潜入冷水,这并不困难。

这是一个发送带附件的电子邮件的工作示例,一旦您了解MIME您可以使用邮件做任何您想做的事情。

# -*- coding: iso-8859-1 -*-
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP

msg = MIMEMultipart()
msg['Subject'] = 'Email From Python'
msg['From'] = 'sai@gmail.com'
msg['To'] = 'whatever@whatever.com'

# That is what u see if dont have an email reader:
msg.preamble = 'Multipart massage.\n'

# This is the textual part:
part = MIMEText("Hello im sending an email with a PDF from a python program")
msg.attach(part)

# This is the binary part(The Attachment):
part = MIMEApplication(open("networkanalyze.pdf","rb").read())
part.add_header('Content-Disposition', 'attachment', filename="file.pdf")
msg.attach(part)

# Create an instance in SMTP server
smtp = SMTP("smtp.gmail.com:587")
smtp.ehlo()
smtp.starttls()
smtp.login("sai@gmail.com", "mySuperSecretPassword")
smtp.close()

# Send the email
smtp.sendmail(msg['From'], msg['To'], msg.as_string())