Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.41 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. """
  3. send an email through gmail
  4.  
  5. Save this script to send-gmail.py,
  6. place the body of the email in email_message.txt,
  7. and then run:
  8.  
  9.     python send-gmail.py \
  10.             --wait 5 \
  11.             --user me@gmail.com \
  12.             --pass 'my password' \
  13.             --to 'random person <recipient1@example.com>' \
  14.             --to 'other random person <recipient2@example.com>' \
  15.             --subject 'this is the email subject' \
  16.             --body email_message.txt
  17.  
  18.  
  19. which means:
  20.     wait 5 minutes
  21.     then send an email from me@gmail.com
  22.     to recipient1@example.com and recipient2@example.com
  23.     with the subject "this is the email subject"
  24.     and the message from email_message.txt
  25.  
  26.  
  27. """
  28.  
  29. import os
  30. import sys
  31. import optparse
  32. import smtplib
  33. import time
  34.  
  35. from email.MIMEMultipart import MIMEMultipart
  36. from email.MIMEText import MIMEText
  37.  
  38.  
  39. def create_message(user, recipients, subject, body):
  40.     msg = MIMEMultipart()
  41.     msg['From'] = user
  42.     msg['To'] = ', '.join(recipients)
  43.     msg['Subject'] = subject
  44.     msg.attach(MIMEText(body))
  45.     return msg
  46.  
  47.  
  48. def send_mail(user, password, recipients, subject, body):
  49.     msg = create_message(user, recipients, subject, body)
  50.  
  51.     server = smtplib.SMTP('smtp.gmail.com', 587)
  52.     server.ehlo()
  53.     server.starttls()
  54.     server.ehlo()
  55.     server.login(user, password)
  56.     server.sendmail(user, recipients, msg.as_string())
  57.     server.close()
  58.     print('Sent email to %s' % (', '.join(recipients)))
  59.  
  60.  
  61. def parse_args():
  62.     parser = optparse.OptionParser()
  63.  
  64.     parser.add_option('-u', '--user', dest='user',
  65.                       default=None, help='gmail account')
  66.  
  67.     parser.add_option('-p', '--pass', dest='password',
  68.                       default=None, help='gmail password')
  69.  
  70.     parser.add_option('-b', '--body', dest='body',
  71.                       default=None, help='email message txt file')
  72.  
  73.     parser.add_option('-s', '--subject', dest='subject',
  74.                       default=None, help='email subject')
  75.  
  76.     parser.add_option('-t', '--to', dest='recipients',
  77.                       action='append', default=[],
  78.                       help='specify a recipient')
  79.  
  80.     parser.add_option('-w', '--wait', dest='wait',
  81.                       default=None, help='seconds to wait before sending')
  82.  
  83.     opts, args = parser.parse_args()
  84.  
  85.     if not opts.user:
  86.         parser.error("specify sender address with --user 'me@gmail.com'")
  87.  
  88.     if not opts.password:
  89.         parser.error("specify sender's password with --pass 'my password'")
  90.  
  91.     if not opts.body:
  92.         parser.error('specify a text file with the message text as --body')
  93.  
  94.     if not opts.subject:
  95.         parser.error("specify a subject with --subject 'my subject'")
  96.  
  97.     if not opts.recipients:
  98.         parser.error('specify at least one recipient with --to')
  99.  
  100.     if not os.path.exists(opts.body):
  101.         parser.error('oops! %s does not exist' % (opts.body))
  102.  
  103.     return opts
  104.  
  105.  
  106. def main():
  107.     opts = parse_args()
  108.  
  109.     fp = open(opts.body)
  110.     body = fp.read()
  111.     fp.close()
  112.  
  113.     if opts.wait is not None:
  114.         now = time.time()
  115.         future = now + (float(opts.wait) * 60.)
  116.         print 'waiting %.2f minutes...' % (float(opts.wait))
  117.         while now < future:
  118.             sys.stdout.write('.')
  119.             sys.stdout.flush()
  120.             time.sleep(1.)
  121.             now = time.time()
  122.         sys.stdout.write('\ndone waiting...\n')
  123.  
  124.     send_mail(opts.user, opts.password,
  125.               opts.recipients,
  126.               opts.subject, body)
  127.  
  128. main()