send mail to gmail

send mail to gmail

Postby len9780 » Thu Jun 02, 2016 9:15 am

is there anyone know how to send mail to gmail with python via smtp ?
I've search lot and got problem when i tried to use the "login " function or "sendmail" function?
help me please
Last edited by Yoriz on Thu Jun 02, 2016 11:49 am, edited 1 time in total.
Reason: First post lock.
len9780
 
Posts: 1
Joined: Thu Jun 02, 2016 9:10 am

Re: send mail to gmail

Postby snippsat » Sat Jun 04, 2016 11:42 pm

is there anyone know how to send mail to gmail with python via smtp ?

Yes,they port 587 way worked for a long time.
Read this.

There are also many modules that deal with this,
search PyPi, Github and Google also work ;)
We will be moving to python-forum.io on October 1 2016
User avatar
snippsat
 
Posts: 1251
Joined: Thu Feb 21, 2013 12:04 am

Re: send mail to gmail

Postby mariareese » Tue Sep 20, 2016 8:22 am

May be it will also helpful for you ..:)

Code: Select all
def send_email(user, pwd, recipient, subject, body):
    import smtplib

    gmail_user 
= user
    gmail_pwd 
= pwd
    FROM 
= user
    TO 
= recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT 
= body

    
# Prepare actual message
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """
 % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print 'successfully sent the mail'
    except:
        print "failed to send mail"


if you want to use Port 465 you have to create an SMTP_SSL object:

Code: Select all
# SMTP_SSL Example
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(gmail_user, gmail_pwd)  
# ssl server doesn't support or need tls, so don't call server_ssl.starttls() 
server_ssl.sendmail(FROM, TO, message)
#server_ssl.quit()
server_ssl.close()
print 'successfully sent the mail'
Last edited by micseydel on Tue Sep 20, 2016 3:57 pm, edited 1 time in total.
Reason: Code tags. Initial post lock.
Maria
mariareese
 
Posts: 1
Joined: Tue Sep 20, 2016 8:14 am


Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests