Friday, October 17, 2014

Sending email from a gmail account using python

In this guide I'll walk you through the python commands that you can use to send an email to someone from your gmail account. If you don't have python set it up in your environment look here for detail. Enter the commands step by step.
[shazni@shazniInWSO2 bin]$ python
Python 2.7.5 (default, Jun 25 2014, 10:19:55)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fromAddress="aaaaaaaaaaaa@gmail.com"
>>> toAddress="bbbbbbbbbbbb@yahoo.com"
>>> msg="Subject: Hello\n\nHi!! How are you"
>>> import smtplib
>>> server=smtplib.SMTP("smtp.gmail.com", 587)
>>> server.starttls()
(220, '2.0.0 Ready to start TLS')
>>> password="xxxxxxxxxxxxxx"
>>> server.login(fromAddress, password)
(235, '2.7.0 Accepted')
>>> server.sendmail(fromAddress, toAddress, msg)
{}
Replace 'aaaaaaaaaaaa', 'bbbbbbbbbbbb' and 'xxxxxxxxxxxxxx' accordingly.

You can easily put this into a python script and invoke it and send a quick email to someone from the command line without ever opening your email account in a browser. Following is a sample script which does the same as above.
#!/usr/bin/python

import smtplib
import getpass

fromAddress = raw_input("Enter your gmail address: ")
toAddress = raw_input("Enter the recipients email address: ")
subject = raw_input('Enter the subject of email: ')
bodyText = raw_input('Enter the body text: ')

msg = "Subject: " + subject + "\n\n" + bodyText

#msg="Subject: Hello\n\nHi!! How are you"

server=smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()

password = getpass.getpass('Gmail Password: ')
server.login(fromAddress, password)
server.sendmail(fromAddress, toAddress, msg)
A sample run of the script is shown below.
[shazni@shazniInWSO2 Python]$ python sendEmail.py
Enter your gmail address: mshazninazeer@gmail.com
Enter the recipients email address: mshazninazeer@yahoo.com   
Enter the subject of email: Hello
Enter the body text: This is a sample text sent to you by python!!!
Gmail Password:
[shazni@shazniInWSO2 Python]$
Enjoy using python script to send emails

22 comments:

  1. Traceback (most recent call last):
    File "./email.test.py", line 19, in
    server.login(fromAddress, password)
    File "/usr/lib/python2.7/smtplib.py", line 615, in login
    raise SMTPAuthenticationError(code, resp)
    smtplib.SMTPAuthenticationError: (534, '5.7.14 Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 141sm4582829ioz.39 - gsmtp')

    ReplyDelete
  2. I tried this using python2.7 and it works for me. This will only work for a gmail account. Also this may not work for an account where 2-step verification is enabled. Make sure you enter the passowrd correctly.

    ReplyDelete

  3. raise SMTPAuthenticationError(code, resp)
    smtplib.SMTPAuthenticationError.....

    It is because Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:

    Go to this link and select Turn On

    https://www.google.com/settings/security/lesssecureapps

    More on...smtp mail

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. Buyers Reviews defines personal characteristics of

    different health products including skincare,weightloss,muscle and male enhancement.Buyers Reviews

    is supplying 100% original and accurate information at each moment of time around our site and

    merchandise, and the intent is to improve the usage of good and pure health supplement.

    ReplyDelete


  10. Pinterest

    Instagram

    Twitter/


    Supplements Book We have best and natural Product of health & wellness supplement you can get several benefits with Us.Different health products including skincare,weightloss,muscle and male enhancement.

    http://supplementsbook.org

    http://supplementsbook.org/keto-ultra-burn/

    http://supplementsbook.org/keto-power-diet/

    https://sites.google.com/site/supplementsbookk/

    ReplyDelete

  11. Upgrade to Office 365 and get the latest Office applications, installs on multiple PCs/Macs and devices, 1 TB OneDrive cloud storage per user, tech support, and other services with your subscription.

    office.com/setup
    Office.com/Setup

    ReplyDelete
  12. I'll want a separate Libido. Bandox Extreme Testosterone Booster Supplement adds emotional content to matters. I even have to mention I've seen some pretty attention-grabbing things. Libido has legitimate price for a person such as you. Check this out, "Finders keepers, losers weepers." In of these long Testosterone Booster articles, I positive strive to avoid all the fluff.

    https://www.nutrifitweb.com/bandox-extreme/

    https://www.nutrifitweb.com/

    ReplyDelete
  13. If you have enough space you can easily generate your own electricity with the help of Backyard Revolution and reward your self and the world. Once you are happy reading the Backyard Revolution Review its time to get your hands into it.

    ReplyDelete
  14. Want to listen to your eBook before sleeping, or while traveling, or exercising? If you love to read books, then possibly sometimes, you want your device to read them for you.

    Online Printer Install

    ReplyDelete
  15. If you know the courses that help you make money, Kibo Code is one of them. I mean the Kibo code is a very profitable course for e-commerce companies. It is unique and highly predictable due to the business strategies presented in the course. Read this Kibo Code Review carefully so that you can make an easy decision of whether to register in the Kibo Code System or not

    ReplyDelete
  16. This is just what the doctored ordered. Testosterone wasn't a way to escape from the pressures of day to day life. What's more, I may need to admit this Testosterone could be quite an accomplishment anyhow. TestoGen This is not recommended to do both at the same occasion. It is just the nature of Testosterone. This is like attempting to place a square peg into a round hole.

    https://www.topbodyproducts.com/testogen/

    https://www.topbodyproducts.com/

    ReplyDelete
  17. Fat-burning foods help you to have increased metabolism and help you in reducing appetite as well. While reducing weight continues to be a challenge for many, consuming fat burning foods can help you to lose weight to a significant extent.

    ReplyDelete

  18. In a recent ballot , respondants have been requested to select the well-being which nice healthy their description of wellness. Can someone else feel the affection for health as I do? it's very clean this ultimately well being turns into much less famous. i believe that those are doable plans.

    https://www.nutrahealthpro.com/annabiol-cbd-oil/

    https://www.facebook.com/nutrahealthpro/posts/195489155624219

    https://twitter.com/nutrahealthpro/status/1348655112005144577

    https://in.pinterest.com/pin/596867756862904845/

    https://www.instagram.com/p/CJ6PpanhLE4/

    ReplyDelete

  19. How do experts show up upon transcendent health directories? wellbeing is possible scholars and it is proper in the front of you. that is how to instantly use wellbeing. they will be returned. that is a scandal. I received some of "yes" solutions to my wellbeing query, even though also a shockingly big variety of "no" answers.
    https://www.nutrahealthpro.com/annabiol-cbd-oil/

    https://www.facebook.com/nutrahealthpro/posts/195489155624219

    https://twitter.com/nutrahealthpro/status/1348655112005144577

    https://in.pinterest.com/pin/596867756862904845/

    https://www.instagram.com/p/CJ6PpanhLE4/

    ReplyDelete

  20. if you be a part of in with outsiders, you can undertake their health behavior. It became a hard hitting analysis of wellness. i might be astounded to find this to be proper when it comes to wellbeing a yr from today. With well-being, the whole lot else comes together. you may attain it in your spare time. this is sort of clean.

    https://www.nutrahealthpro.com/annabiol-cbd-oil/

    https://www.facebook.com/nutrahealthpro/posts/195489155624219

    https://twitter.com/nutrahealthpro/status/1348655112005144577

    https://in.pinterest.com/pin/596867756862904845/

    https://www.instagram.com/p/CJ6PpanhLE4/

    ReplyDelete