I am trying to send e-mail with multiple attachements via g-mail. The names of files I want to send every day are changing their names each month. Do you know any solution for this?
I am also getting and error code TimeoutError: [WinError 10060] despite using specific app password generated from gmail.
I would be really grateful for any help. Thank you.
from posixpath import basename
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
send_from = "EMAIL-1"
send_to = "EMAIL-2"
subject = "My Subject"
text = "This is an e-mail."
files = [r'PATH1', r'PATH2']
def _send_email(send_from, send_to, subject, text, files=None):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
part['Content-Disposition'] = 'attachment; filename="%s"' %basename(f)
msg.attach(part)
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(send_from, 'GENERATED PASSWORD')
smtpObj.sendmail(send_from, send_to, msg.as_string())
smtpObj.quit()
_send_email(send_from, send_to, subject, text, files)