~oly/python-snippets/pygame-point-in-triangle-snippet

« back to all changes in this revision

Viewing changes to email/mail.py

  • Committer: Jono Bacon
  • Date: 2010-04-24 18:34:32 UTC
  • mfrom: (81.11.2 webkit)
  • Revision ID: jono@system76-pc-20100424183432-tguvcjol24lv5alc
SendingĀ emailĀ snippet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# [SNIPPET_NAME: Sending Email]
 
2
# [SNIPPET_CATEGORIES: smtplib, email]
 
3
# [SNIPPET_DESCRIPTION: Sending mail from gmail account with many attachements and to_many_mails_ids]
 
4
# [SNIPPET_AUTHOR: kutuma]
 
5
# [SNIPPET_LICENSE: GPL]
 
6
# [SNIPPET_UPLOADED_BY: Arulalan.T <tarulalan@gmail.com>]
 
7
 
 
8
# you need to set the gmail user name and its password at the line of 22 and 23 st (in gedit, line number) of this snippet.
 
9
 
 
10
# you need to set the to_mail_ids in a string array , subject, body , attachements_absolute_path in a string array from the line of 26 of this snippet.
 
11
 
 
12
 
 
13
#!/usr/bin/python
 
14
# ref : http://kutuma.blogspot.com/2007/08/sending-emails-via-gmail-with-python.html
 
15
import smtplib
 
16
from email.MIMEMultipart import MIMEMultipart
 
17
from email.MIMEBase import MIMEBase
 
18
from email.MIMEText import MIMEText
 
19
from email import Encoders
 
20
import os
 
21
 
 
22
gmail_user = "username@gmail.com"
 
23
gmail_pwd = "gmail_password"
 
24
 
 
25
 
 
26
body=" I am Body "
 
27
subject="I   am     Subject "
 
28
 
 
29
to_mail_ids=["friend1_mail_id","friend2_mail_id"]
 
30
attachements_path=["absolute_attachement1_with_extension","absolute_attachement2_with_extension"]# for eg : /home/arul/z/nnk.txt, /home/arul/python/sam.py
 
31
 
 
32
 
 
33
 
 
34
def mail(to, subject, text, attach=[]):
 
35
   msg = MIMEMultipart()
 
36
 
 
37
   msg['From'] = gmail_user
 
38
   msg['To'] = to
 
39
   msg['Subject'] = subject
 
40
 
 
41
   msg.attach(MIMEText(text))
 
42
 
 
43
   try:
 
44
 
 
45
                for i in range(len(attach)):
 
46
                                part = MIMEBase('application', 'octet-stream')
 
47
                                part.set_payload(open(attach[i], 'rb').read())
 
48
                                Encoders.encode_base64(part)
 
49
                                part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(str(attach[i])))
 
50
                                msg.attach(part)
 
51
   except:
 
52
                print " The attachments doesnt exist in the path %s and %s" % (attach[0],attach[1])
 
53
                        
 
54
 
 
55
   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
 
56
   mailServer.ehlo()
 
57
   mailServer.starttls()
 
58
   mailServer.ehlo()
 
59
   mailServer.login(gmail_user, gmail_pwd)
 
60
   mailServer.sendmail(gmail_user, to, msg.as_string())
 
61
   # Should be mailServer.quit(), but that crashes...
 
62
   mailServer.close()
 
63
 
 
64
 
 
65
 
 
66
for to_mail_id in (to_mail_ids):
 
67
   mail(to_mail_id,
 
68
   subject,
 
69
   body,
 
70
   attachements_path)
 
71
   print "mail sent to :"+to_mail_id
 
72
 
 
73
print "\nmail sent successfully to all\n"