Created
August 1, 2012 13:12
-
-
Save 2xyo/3226727 to your computer and use it in GitHub Desktop.
Small script to send gif by mail...
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
#!/usr/bin/env python | |
import imaplib | |
from pprint import pprint | |
from email.parser import HeaderParser | |
from django.core.mail import EmailMultiAlternatives | |
from email.mime.message import MIMEMessage | |
from django.conf import settings | |
import os,random,sys | |
from email import Charset | |
from email.mime.image import MIMEImage | |
import os.path | |
from email.MIMEBase import MIMEBase | |
from django.core.mail import SafeMIMEMultipart | |
import string | |
os.environ["DJANGO_SETTINGS_MODULE"] = "settings" | |
img_content_cid = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(5)) | |
imaplib.Debug= 4 | |
def getHeader(): | |
mail = imaplib.IMAP4_SSL(settings.IMAP_HOST) | |
mail.login(settings.IMAP_HOST_USER, settings.IMAP_HOST_PASSWORD) | |
mail.list() | |
mail.select("[Gmail]/Tous les messages",readonly=True) # connect to inbox. | |
if len(sys.argv)>1: | |
s = ' '.join(sys.argv[1:]) | |
subject = '(SUBJECT "' + s + '")' | |
result, datan = mail.search(None,subject) | |
else: | |
result, datan = mail.search(None,'(OR (TO "[email protected]") (CC "[email protected]"))') | |
#mail.select("[Gmail]/Tous les messages",readonly=True) # connect to inbox. | |
data = mail.fetch(datan[0].split(' ')[-1], '(BODY[HEADER])') | |
parser = HeaderParser() | |
mail.logout() | |
return parser.parsestr(data[1][0][1]) | |
def getRandomImage(): | |
r = random.choice(os.listdir(settings.GIF_PATH)) | |
img_data = open(settings.GIF_PATH + r, "rb").read() | |
img = MIMEImage(img_data) | |
img.add_header('Content-ID', '<%s>' % img_content_cid) | |
img.add_header('Content-Disposition', 'inline') | |
img.add_header('X-Attachment-Id','%s' % img_content_cid) | |
return img | |
def createMail(img, original): | |
to = [] | |
to.append(original['To'].replace("\r\n","")) | |
to.append(original['From'].replace("\r\n","")) | |
if original['Cc']: | |
to.append(original['Cc'].replace("\r\n","")) | |
Charset.add_charset('ISO-8859-1',Charset.SHORTEST,None,'ISO-8859-1') | |
new = EmailMultiAlternatives( | |
"Re: "+original["Subject"].replace("\r\n",""), | |
"YEAH BABY ! IT'S FIP GIF", | |
settings.EMAIL_HOST_USER, # from | |
to,#to | |
headers = { | |
'Reply-To': "[email protected], [email protected], [email protected]", | |
"In-Reply-To": original["Message-ID"], | |
"References": original["Message-ID"] | |
} | |
) | |
new.attach_alternative( | |
'<html>' | |
+ '<p><img src="cid:' | |
+ img_content_cid | |
+ '" alt="' | |
+ img_content_cid | |
+ '" />' | |
+ '</p><h1>Quentin is GAY</h1></html>', 'text/html' | |
) | |
new.attach(img) | |
new.send() | |
if __name__ == "__main__": | |
print "start" | |
original = getHeader() | |
img = getRandomImage() | |
createMail(img,original) | |
print "End" |
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
EMAIL_USE_TLS = True | |
EMAIL_HOST = 'smtp.gmail.com' | |
EMAIL_PORT = 587 | |
EMAIL_HOST_USER = 'YOUR MAIL' | |
EMAIL_HOST_PASSWORD = 'YOUR PASS' | |
IMAP_HOST = 'imap.gmail.com' | |
IMAP_HOST_USER = 'YOUR MAIL' | |
IMAP_HOST_PASSWORD = 'YOUR PASS' | |
GIF_PATH = '/PATH/OF/GIFS/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment