Guest User

Untitled

a guest
Dec 9th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. #!/usr/bin/python
  2. # sendemail.pl - Sends email based on a conf file or arguments
  3.  
  4. # DOCUMENTATION:
  5. # USAGE:
  6. # Either:
  7. # sendemail.py
  8. # - Checks /mnt/sdcard/tmp/mailout.conf by default
  9. # sendemail.py <conf file location>
  10. # - Specify a conf file location
  11. # sendemail.py <recipient> <subject> <message body>
  12. # - Specify email details manually
  13. #
  14. # EXAMPLE CONF FILE LAYOUT:
  15. # mailto: joe@bloggs.com
  16. # subject: This is the subject
  17. # body:
  18. # This is the body of the message.
  19. # Note that the actual message begins AFTER the first line break after body:
  20. # Obviously don't include the # comments on the left, or the indents.
  21. # Line breaks and spacing should be preserved by most mail clients.
  22. # Most clients will also handle text wrapping automatically.
  23. # This will be read all the way until the end of the file.
  24. #
  25. # QUESTIONS:
  26. # WHY IS THERE A DEFAULT LOCATION OPTION?:
  27. # The reason there's a default conf file location with no arguments is
  28. # because this script was written for use with the Tasker Android app, in
  29. # conjunction with Android Scripting Environment: at this time, Tasker can't
  30. # specify arguments when running a script - You will need to use a Tasker
  31. # File operation to write the file contents before running the script then
  32. # (preferably) clean up by deleting the file once the script has run.
  33. #
  34. # WARGH! MY FILE LOCATION IS DIFFERENT AND I WANT TO USE TASKER!:
  35. # The default location I've entered was tested with my HTC Desire running
  36. # ASE: You'll need to edit this script, and change the following line:
  37. # conf_file = '/mnt/sdcard/tmp/mailout.conf'
  38. # to point to the location of your file. Or wait until Tasker allows you to
  39. # specify arguments when running a script.
  40. #
  41. # WHY DO I NEED TO ENTER MY USERNAME/PASSWORD IN PLAINTEXT?:
  42. # Because there's currently no way authenticate directly with the server and
  43. # send an Email in the current scripting API - You can only call the mail
  44. # app with specific arguments and have the Compose window pop up - manual
  45. # interaction is required to actually send the email - This script bypasses
  46. # this by interacting and authenticating directly with the Google SMTP
  47. # server, but in order to do this, your logic details need to be inside the
  48. # script.
  49. #
  50. # HOW DO I KNOW YOU AREN'T GOING TO STEAL MY DETAILS?:
  51. # Because this script is in plain text and you can see all of the operations
  52. # carried out below these comments? Do you seriously think that I'd try and
  53. # steal your Gmail login details in such an obvious manner? Don't worry,
  54. # the email account linked to your level 80 Dark Elf with epic drops is
  55. # safe :P
  56. #
  57. # I USE GOOGLE APPS, CAN I STILL USE THIS?:
  58. # Yes: Just enter your full Google Apps email address and password in the
  59. # email_user and email_pass values.
  60. #
  61. # I WANT TO USE ANOTHER SMTP SERVER!:
  62. # Then just edit the smtp_server and smtp_port lines to match your SMTP
  63. # details.
  64. # I've attempted to add support for SMTP over SSL as well as Google's
  65. # default TLS option, but it's vastly untested. I've also not tested this
  66. # using plain old unsecure/unauthenticated SMTP, but I really wouldn't
  67. # advise using that anyway, and probably change to a better mail host if
  68. # they didn't offer some secure method of connecting.
  69. # If your mail provider only offers POP before SMTP authentication, then
  70. # there's not much I can do there for you.
  71. #
  72. # YOUR PYTHON IS SHIT!:
  73. # That is not a question. And this is my first ever Python script. Blow me.
  74.  
  75. # Prints out usage instructions
  76. def printUsage():
  77. print "USAGE:"
  78. print " sendemail.py"
  79. print " - Checks /mnt/sdcard/tmp/mailout.conf by default"
  80. print " sendemail.py <conf file location>"
  81. print " - Specify a conf file location"
  82. print " sendemail.py <recipient> <subject> <message body>"
  83. print " - Specify email details manually"
  84.  
  85. # Prints out an example conf file layout
  86. def confFileLayout():
  87. print "EXAMPLE CONF FILE LAYOUT:"
  88. print "mailto: joe@bloggs.com"
  89. print "subject: This is the subject"
  90. print "body:"
  91. print "This is the body of the message."
  92. print "Note that the actual message begins AFTER the first line break after body:"
  93. print "Line breaks and spacing should be preserved by most mail clients."
  94. print "Most clients will also handle text wrapping automatically."
  95. print "This will be read all the way until the end of the file."
  96.  
  97. # Sends the actual email!
  98. def sendemail(mailto,subject,body):
  99. import smtplib
  100.  
  101. # CHANGE THESE!
  102. email_name = '' # Optional - A friendly name for the 'From' field
  103. email_user = '' # Enter your email
  104. email_pass = '' # and password
  105.  
  106. # DON'T CHANGE THIS!
  107. # ...unless you're rewriting this script for your own SMTP server!
  108. smtp_server = 'smtp.gmail.com'
  109. smtp_port = 587
  110.  
  111. # Build an SMTP compatible message from arguments
  112. if (email_name is not ''):
  113. msg = "From: " + email_name + " <" + email_user + ">\n"
  114. else:
  115. msg = "From: " + email_user + "\n"
  116. msg += "To: " + mailto + "\n"
  117. msg += "Subject: " + subject + "\n"
  118. msg += body
  119.  
  120. # Attempt to connect and send the email!
  121. try:
  122. smtpObj = '' # Declare within this block.
  123. # Check for SMTP over SSL by port number and connect accordingly
  124. if( smtp_port == 465):
  125. smtpObj = smtplib.SMTP_SSL(smtp_server,smtp_port)
  126. else:
  127. smtpObj = smtplib.SMTP(smtp_server,smtp_port)
  128. smtpObj.ehlo()
  129. # StartTLS if using the default TLS port number
  130. if(smtp_port == 587):
  131. smtpObj.starttls()
  132. smtpObj.ehlo
  133. # Login, send and close the connection.
  134. smtpObj.login(email_user,email_pass)
  135. smtpObj.sendmail(email_user,mailto,msg)
  136. smtpObj.close()
  137. return 1 # Return 1 to denote success!
  138. except Exception, err:
  139. # Print error and return 0 on failure.
  140. print err
  141. return 0
  142.  
  143. import re,sys,math,os.path,android
  144.  
  145. # Declare variables...
  146. conf_file = ''
  147. mailto = ''
  148. subject = ''
  149. body = ''
  150.  
  151. #get the intent from Tasker
  152. droid = android.Android()
  153. results = droid.getIntent()[1]
  154. extras = results['extras']
  155. extra_to = extras['%tomail']
  156. extra_subject = extras['%subject']
  157. extra_body = extras['%body']
  158.  
  159.  
  160. # Check arg lengths and set up variables/conf file location accordingly
  161. # Pull out values if set individually
  162. mailto = extra_to
  163.  
  164. # Check email address is valid!
  165. mailre = re.compile('(.+@.+\..+)',re.M)
  166. m = mailre.search(mailto)
  167. if(not m):
  168. print "Email recipient value " + mailto + " not a valid email address!"
  169. sys.exit(1)
  170.  
  171. subject = extra_subject
  172. body = extra_body
  173.  
  174.  
  175. if ( mailto and subject and body ):
  176. # Send email if all values set!
  177. if (sendemail(mailto, subject, body)):
  178. print "Email sent successfully!"
  179. sys.exit(0)
  180. else:
  181. # Exit with error if email is not sent successfully
  182. print "Failed to send email! Check your login details and connection!"
  183. sys.exit(1)
  184. else:
  185. # Print usage details if values not present.
  186. printUsage()
  187. sys.exit(1)
Add Comment
Please, Sign In to add comment