viruszzz

Simple Mail Sender

Jul 6th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # Just simply send an email with out having own SMTP server.
  3. # It searches the first available on the internet
  4. # nice isn't it? :)
  5. #
  6. from sys import argv
  7. from email.mime.text import MIMEText
  8. import smtplib
  9. from DNS.lazy import mxlookup
  10.  
  11. if len(argv) != 5:
  12.   print("Usage : " + argv[0] +" FROM TO SUBJECT MESSAGE")
  13.   print(" FROM is the email address to send from")
  14.   print(" TO is the email address to send to")
  15.   print(" SUBJECT is the email's subject")
  16.   print(" MESSAGE is the message to send")
  17. else:
  18.   msg = MIMEText(argv[4])
  19.   msg['Subject'] = argv[3]
  20.   msg['To'] = argv[2]
  21.   msg['From'] = argv[1]
  22.   try:
  23.     server = mxlookup(argv[2].split('@')[1])[0][1]
  24.   except:
  25.     print("Error : Could not get an MX record.")
  26.     exit(1)
  27.   print("Connecting to " + server)
  28.   s = smtplib.SMTP(server)
  29.   d = s.sendmail(argv[1], [argv[2]], msg.as_string())
  30.   if len(d) == 0:
  31.     print("Email sent successfully")
  32.   else:
  33.     print("Error occured while sending : " + repr(d))
  34.   s.quit()
  35.   exit(0)
Advertisement
Add Comment
Please, Sign In to add comment