Problem to send an email using MIME
My name is Stan and i am a mathematic teacher.
i want to send 30 different pdf to 30 different students ( one file to each student)
I have a code using the module MIME
that i founded online and that i modificated a bit adding a loop. i put this code attached to this message.
But my problem is that this code uses the path of a file like \ users\Stan\dropbox....
So it’s working good on a mac but not on the ipad.
i would like to know if there is a way to make it work on ipad ? i am quite a newbie in coding.
thank you very much in advance.
# -*- coding: utf-8 -*- import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders fromaddr = '''my mail adress''' classe=input('enter the class: ts ou sec ou spe') if classe=="ts": toaddrs='''list of the mail adresses''' filenames = '''list of my files''' paths='''list of the paths of the files like : \users\jean......''' n=len(paths) if classe=='sec': toaddrs='''list of the mail adresses''' filenames = '''list of my files''' paths='''list of the paths of the files like : \users\jean......''' n=len(paths) if classe=="spe": toaddrs='''list of the mail adresses''' filenames = '''list of my files''' paths='''list of the paths of the files like : \users\jean......''' n=len(paths) for k in range(n): msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddrs[k] msg['Subject'] = '''the subject''' body = '''body of the mail''' msg.attach(MIMEText(body, 'plain')) attachment = open(paths[k], "rb") filename=filenames[k] toaddr=toaddrs[k] part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % filename) msg.attach(part) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fromaddr, '''my password''') text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit()
Pythonista's main "Script Library" folder (where you have your scripts etc.) has a very long name, which is randomly generated by iOS and is different on every device. This makes it impossible to type out the full paths like you would on a normal computer.
There is a way around this problem though: using the
os.path.expanduserfunction, you can get the full path of a file in this randomly named folder. First, put
import osat the top of your script so you can use the
osmodule. Now you can use
os.path.expanduser("~/Documents")to get the path to Pythonista's "Script Library" folder. This means that if you have a file "Assignment1.pdf" in the Script Library, you can get its path using
For example, if your
pathslist currently looks like this:
paths = [ "/Users/Jean/Dropbox/Assignment1.pdf", "/Users/Jean/Dropbox/Assignment2.pdf", ]
you would change it to
paths = [ os.path.expanduser("~/Documents/Assignment1.pdf"), os.path.expanduser("~/Documents/Assignment2.pdf"), ]
Amazing ! It works perfectly !
Thank you very much dgelessus to have taken time to write the answer. It solved my problem.
This approach works very well, except I ran into a problem when I tried to send emails to 30+ students. After approx 25 emails, the remaining emails failed-to-send. When I login to Gmail, the message seems to have bounced back, meaning the message isn't sitting in my Drafts folder, but rather sitting in Inbox as a returned message.
I thought this might be a problem at the recipient's end --- all messages are going to different students with the same domain name.
However, now, when I try to send just a regular email to anyone (not necessarily a student at the same domain name), the account fails to send message.
Obviously, I'm missing something here. Any help would be much appreciated.
- I have given permission within Gmail for "less secure" clients to use my Gmail account.
- I do not have two-factor authentication turned on.
You seem to be missing the ehlo/helo, which i think google uses to help prevent ddos. also, if you didnt have a time.sleep in there, you may have been flagged as a bad user. you are limited to 20 messages per second or something. also, i think google prefers that you connect to one session to send multiple messages rather than connect once per message.
check those bounce messages to see if there are any errors shown.
@JonB that worked! For the future users, I had already moved the session connection and termination outside the loop; so, that wasn't the problem.
ehloand added a 10 second pause between messages using
time.sleep(). One or both resolved the issue.