Python Forum
How to send email using python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send email using python?
#1
I am trying to send a email using Python. I have an account on Gmail and a proven password (which I will say as ABCD00). When I try to send the email, I get an error:

import smtplib, ssl

sender_email = "my@gmail"
receiver_email = "[email protected]"
message = "Hello, world"

ctx=ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=ctx) as server:
   server.login("[email protected]", "ABCD00")
   server.sendmail(sender_email, receiver_email, message)
But I get:
Error:
Traceback (most recent call last): File "./emailer.py", line 15, in <module> server.login("[email protected]", "ABCD00") File "/usr/lib/python3.5/smtplib.py", line 729, in login raise last_exception File "/usr/lib/python3.5/smtplib.py", line 720, in login initial_response_ok=initial_response_ok) File "/usr/lib/python3.5/smtplib.py", line 641, in auth raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials y22sm7196990ejf.108 - gsmtp')
Same thing if I use my as user name. Yet, I can log in with [email protected] and ABCD00 on the browser.
What is the error? How can I send an email with Python?
Thank you
Reply
#2
Go into your google account in Safety turn on Access to less secure apps.
Then try again.
Reply
#3
Now that I have removed the safeties from Gmail, I can send the email:
import smtplib, ssl

sender_email = "me@gmail"
receiver_email = "[email protected]"
message = "Hello, world"

print("sending email")
ctx=ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=ctx) as server:
    server.login("me", password)
    server.sendmail(sender_email, receiver_email, message)
print("sent")
Probably there is a better way, in particular without the use of SSL (but when I tried smtplit.SMTP only, it did not work.

My question now is: how can I add a subject to the email?
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 678 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,264 Nov-14-2022, 09:31 PM
Last Post: malonn
  Unable to send email attachments cosmarchy 7 2,442 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  How to make scraper send email notification just once themech25 0 1,362 Nov-08-2021, 01:51 PM
Last Post: themech25
  send email smtp rwahdan 0 1,754 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  How can I get Python Bulk Email Verification Script With API? zainalee 1 2,460 Jun-06-2021, 09:19 AM
Last Post: snippsat
  Need Outlook send email code using python srikanthpython 3 8,086 Feb-28-2021, 01:53 PM
Last Post: asyswow64
Video Python Bulk Email Verification Script With API Aj1128 0 2,595 Nov-28-2020, 11:38 AM
Last Post: Aj1128
  Sending Out Email via Python JoeDainton123 1 4,699 Aug-31-2020, 12:54 AM
Last Post: nilamo
  I am trying to send an email with python nick235 9 5,201 Jun-29-2020, 06:40 PM
Last Post: nick235

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020