Created
December 1, 2015 04:26
-
-
Save SlimeQ/b73dc36b844d673223d9 to your computer and use it in GitHub Desktop.
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
# Import smtplib for the actual sending function | |
import smtplib | |
# Here are the email package modules we'll need | |
from email.mime.image import MIMEImage | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
COMMASPACE = ', ' | |
def sendfile(file, toaddr='[email protected]'): | |
try: | |
# Create the container (outer) email message. | |
msg = MIMEMultipart() | |
msg['Subject'] = ''; | |
# me == the sender's email address | |
# family = the list of all recipients' email addresses | |
fromaddr = '[email protected]' | |
msg['From'] = fromaddr | |
msg['To'] = toaddr | |
# body = MIMEText('<p>'+file.split('/')[-2:]+'<img src="cid:'+file.split('/' )[-1]+'" /></p>', _subtype='html') | |
# msg.attach(body) | |
# msg.preamble = file | |
msg['Subject'] = file | |
# Open the files in binary mode. Let the MIMEImage class automatically gues s the specific | |
# image type. | |
with open(file, 'rb') as fp: | |
img = MIMEImage(fp.read(), file.split('.')[-1]) | |
img.add_header('Content-Type', 'image/jpeg; name="' + file.split('/')[-1] + '"') | |
img.add_header('Content-Disposition', 'attachment; filename="'+file.split( '/')[-1]+'"') | |
msg.attach(img) | |
# The below code never changes, though obviously those variables need valu es. | |
session = smtplib.SMTP('smtp.gmail.com', 587) | |
session.ehlo() | |
session.starttls() | |
session.login('username', 'password') | |
session.sendmail(fromaddr, toaddr, msg.as_string()) | |
session.quit() | |
except: | |
print "email failed" | |
if __name__ == '__main__': | |
sendfile('image.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment