Why is Python smtplib not working for Google? - TagMerge
4Why is Python smtplib not working for Google?Why is Python smtplib not working for Google?

Why is Python smtplib not working for Google?

Asked 1 years ago
0
4 answers

Gmail on port 587 does not permit connecting over SSL. You should generally use SMTP, not SMTP_SSL, when connecting to port 587. The SSL-enabled port is usually 465 (but as you note, I don't think Gmail supports that).

Source: link

0

>>> from smtplib import SMTP
>>> with SMTP("domain.org") as smtp:
...     smtp.noop()
...
(250, b'Ok')
>>>
import smtplib

def prompt(prompt):
    return input(prompt).strip()

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while True:
    try:
        line = input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

print("Message length is", len(msg))

server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Source: link

0

These connections are really simple to create with smtplib. The unencrypted version can be created with:
import smtplib

try:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
except:
    print 'Something went wrong...'
There are a few different ways you can secure your SMTP connections in the smtplib library. The first way is to first create an insecure connection and then upgrading to TLS. This is done using the .starttls() method.
import smtplib

try:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    # ...send emails
except:
    print 'Something went wrong...'
Your other option is to create an SSL connection right from the start. In this case, you'll want to use the .SMTP_SSL() method instead:
import smtplib

try:
    server_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server_ssl.ehlo()   # optional
    # ...send emails
except:
    print 'Something went wrong...'
Emails, at the very core, are just strings of text connected by newline characters. Most emails will at least have the "From", "To", "Subject" and a body fields. Here is a simple example:
From: [email protected]
To: [email protected], [email protected]
Subject: OMG Super Important Message

Hey, what's up?

- You
A simple way to parameterize these fields is to use string formatting in Python:
sent_from = '[email protected]'
to = ['[email protected]', '[email protected]']
subject = 'OMG Super Important Message'
body = 'Hey, what's up?\n\n- You'

email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % (sent_from, ", ".join(to), subject, body)

Source: link

0

Before I start talking about this incredibly easy way to send an email with python, I want to start by showing you the code just to give you a sense of how simple, short, and easy the code actually is.
import smtplib

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("your username", "your password")
server.sendmail(
  "from@address.com", 
  "to@address.com", 
  "this message is from python")
server.quit()
In our case, the machine running the SMTP server is the smtp.gmail.com and we want our client application (running on our laptop) to be able to communicate with that server.
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
Let’s get back to our code.
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
This function tries to connect to the SMTP server that lives in smtp.gmail.com and is listening to port 465 over a secure encrypted SSL channel. It returns an smtp object that is referred to by the variable named server.
server.login("your username", "your password")
Now let’s get to the most interesting part, sending the email.
server.sendmail(
  "from@address.com", 
  "to@address.com", 
  "this message is from python")

Source: link

Recent Questions on python

    Programming Languages