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.