Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import smtplib
  4. from email.MIMEMultipart import MIMEMultipart
  5. from email.MIMEBase import MIMEBase
  6. from email.MIMEText import MIMEText
  7. from email import Encoders
  8. import os
  9.  
  10. gmail_user = "@gmail.com"
  11. gmail_pwd = "password"
  12.  
  13. def mail(to, subject, text, attach):
  14.    msg = MIMEMultipart()
  15.  
  16.    msg['From'] = gmail_user
  17.    msg['To'] = to
  18.    msg['Subject'] = subject
  19.  
  20.    msg.attach(MIMEText(text))
  21.  
  22.    part = MIMEBase('application', 'octet-stream')
  23.    part.set_payload(open(attach, 'rb').read())
  24.    Encoders.encode_base64(part)
  25.    part.add_header('Content-Disposition',
  26.            'attachment; filename="%s"' % os.path.basename(attach))
  27.    msg.attach(part)
  28.  
  29.    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
  30.    mailServer.ehlo()
  31.    mailServer.starttls()
  32.    mailServer.ehlo()
  33.    mailServer.login(gmail_user, gmail_pwd)
  34.    mailServer.sendmail(gmail_user, to, msg.as_string())
  35.    # Should be mailServer.quit(), but that crashes...
  36.    mailServer.close()
  37.  
  38. mail("receptor@gmail.com",
  39.    "Hello from python!",
  40.    "This is a email sent with python",
  41.    "my_picture-ocualquiertipodearchivo.jpg")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement