Created
January 3, 2019 06:43
-
-
Save tvdsluijs/ae79133ad151f5a675e58ee9152522c7 to your computer and use it in GitHub Desktop.
Send files with gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
author: Pure Python | |
url: https://www.purepython.org | |
copyright: CC BY-NC 4.0 | |
creation date: 02-01-2019 | |
Small script to send mails with attachment from folder with Gmail | |
Uses nothing but python 3 modules. | |
Create a folder named mail_files and within this folder done and new. | |
You can place all the attachments within the new folder. | |
When the script is ready all attachments send will be in the Done folder. | |
If the script crashes, you can rerun the script as it will start | |
where it stopped. | |
You need an gmail app password for this to work | |
https://support.google.com/accounts/answer/185833?hl=en | |
""" | |
import os | |
import time | |
import smtplib | |
from email.mime.base import MIMEBase | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from email import encoders | |
class MailFiles: | |
def __init__(self, from_addr=None, pass_wd=None, to_addr=None): | |
""" | |
init function for this to work | |
:param from_addr: your gmail account email address | |
:param pass_wd: your gmail app pwd (see discription above) | |
:param to_addr: here the attachment should go to | |
""" | |
self.from_addr = from_addr | |
self.pass_wd = pass_wd | |
self.to_addr = to_addr | |
dir_path = os.path.dirname(os.path.realpath(__file__)) | |
self.start_dir = os.path.join(dir_path, "mail_files/new") | |
self.done_dir = os.path.join(dir_path, "mail_files/done") | |
self.process_files() | |
def process_files(self): | |
""" | |
This will loop thru the files in the New folder and calls the | |
mail_file function. It has a sleep function so Gmail will not | |
be flooded. | |
""" | |
i = 1 | |
for filename in os.listdir(self.start_dir): | |
file = os.path.join(self.start_dir, filename) | |
if self.mail_file(file, filename): # Mailing retuns True or False | |
backup_file = os.path.join(self.done_dir, filename) | |
os.rename(file, backup_file) # move done file to done folder | |
print("Document {} send!".format(filename)) | |
i += 1 | |
time.sleep(5) # Sleep for 5 seconds to not overrun Gmail | |
def mail_file(self, file=None, filename=None): | |
""" | |
This function will mail the file to the email address as an attachment | |
:param file: path with filename | |
:param filename: filename online | |
:return: true or false | |
""" | |
try: | |
msg = MIMEMultipart() | |
msg['From'] = self.from_addr | |
msg['To'] = self.to_addr | |
msg['Subject'] = "Sending file {}".format(filename) | |
body = "Sending File {} to you".format(filename) | |
msg.attach(MIMEText(body, 'plain')) | |
attachment = open(file, "rb") | |
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(self.from_addr, self.pass_wd) | |
text = msg.as_string() | |
server.sendmail(self.from_addr, self.to_addr, text) | |
server.quit() | |
return True | |
except smtplib.SMTPException as e: | |
print(e) | |
return False | |
except: | |
return False | |
if __name__ == '__main__': | |
from_addr = "some@gmail.com" # your gmail account email address | |
pass_wd = "yourpasswors" # your pwd (see discription above) | |
to_addr = "to@emailaddress.com" # where the attachment should go to | |
m = MailFiles(from_addr, pass_wd, to_addr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment