Created
January 16, 2012 19:28
-
-
Save shreyansb/1622514 to your computer and use it in GitHub Desktop.
sample code to send a gmail using python
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/python | |
import smtplib | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email import Encoders | |
import os | |
gmail_user = "[email protected]" | |
gmail_pwd = "password" | |
from_gmail_user = "Social Register <[email protected]>" | |
def mail(to, subject, text, cc=None, bcc=None, reply_to=None, attach=None, | |
html=None, pre=False, custom_headers=None): | |
msg = MIMEMultipart() | |
msg['From'] = from_gmail_user | |
msg['To'] = to | |
msg['Subject'] = subject | |
to = [to] | |
if cc: | |
# cc gets added to the text header as well as list of recipients | |
if type(cc) in [str, unicode]: | |
msg.add_header('Cc', cc) | |
cc = [cc] | |
else: | |
cc = ', '.join(cc) | |
msg.add_header('Cc', cc) | |
to += cc | |
if bcc: | |
# bcc does not get added to the headers, but is a recipient | |
if type(bcc) in [str, unicode]: | |
bcc = [bcc] | |
to += bcc | |
if reply_to: | |
msg.add_header('Reply-To', reply_to) | |
# Encapsulate the plain and HTML versions of the message body in an | |
# 'alternative' part, so message agents can decide which they want to | |
# display. | |
if pre: | |
html = "<pre>%s</pre>" % text | |
if html: | |
msgAlternative = MIMEMultipart('alternative') | |
msg.attach(msgAlternative) | |
msgText = MIMEText(text) | |
msgAlternative.attach(msgText) | |
# We reference the image in the IMG SRC attribute by the ID we give it | |
# below | |
msgText = MIMEText(html, 'html') | |
msgAlternative.attach(msgText) | |
else: | |
msg.attach(MIMEText(text)) | |
if attach: | |
part = MIMEBase('application', 'octet-stream') | |
part.set_payload(open(attach, 'rb').read()) | |
Encoders.encode_base64(part) | |
part.add_header('Content-Disposition', | |
'attachment; filename="%s"' % os.path.basename(attach)) | |
msg.attach(part) | |
if custom_headers: | |
for k, v in custom_headers.iteritems(): | |
msg.add_header(k, v) | |
mailServer = smtplib.SMTP("smtp.gmail.com", 587) | |
mailServer.ehlo() | |
mailServer.starttls() | |
mailServer.ehlo() | |
mailServer.login(gmail_user, gmail_pwd) | |
mailServer.sendmail(gmail_user, to, msg.as_string()) | |
# Should be mailServer.quit(), but that crashes... | |
mailServer.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would you be so kind as to provide a license for this code? Adding a comment at the top of the file such as:
I am original author of this code, and this code is in the public domain.
or
I license this code under the CC by 4.0 license (http://creativecommons.org/licenses/by/4.0/).
would be much appreciated.