How to send mail with Python

I am trying to send simple mail using python

import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login(" mymail@gmail.com ", "mypassword") msg = "Hello world" server.sendmail(" mymail@gmail.com ", " mymail@gmail.com ", msg) server.quit() 

But I get this err:

 server.login(" user@gmail.com ", "psw") File "C:\Python\lib\smtplib.py", line 652, in login raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuxb\n5.7.14 4i2u8qU8V3jgf6uGv8da1RAGPJyctRvIFy_kjai6aKVx_B6qVhoz_dzFpvfPC18H-jeM6K\n5.7.14 cnm2HVuq-wr-uw59hD31ms-cxMmnZuq6Z3_liDaDmu8_UqaiUwR4FUiuX2i5pPdQjJzFvv\n5.7.14 4VrEF5XT4ol2iN17gnB_jITpwzsjH9Ox3NCNcfl7SriHr5m7esc15PWI0CG_2CTlyh7RxW\n5.7.14 XhoJPajs8GMd-khOQWUqucywfrfo> 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 ef10sm13614207wjd.49 - gsmtp') 

What should I do?

thanks

+6
source share
4 answers

It seems like you need something that Google is causing the app password.

Basically, you create a 16-digit password that is unique to your application. This password is entered in python instead of the password that you regularly use to log in to your Google account.

This allows you to continue to take advantage of 2-step verification and also use third-party applications such as your own python program.

Here are instructions from Google on how to create such a password for the application: https://support.google.com/accounts/answer/185833?hl=en

+7
source

you can use this code:

 import smtplib session = smtplib.SMTP('smtp.gmail.com', 587) session.ehlo() session.starttls() session.login(' youremail@gmail.com ',' password') headers = "\r\n".join(["from: " + ' youremail@gmail.com ', "subject: " + "test", "to: " + ' contactemail@gmail.com ', "mime-version: 1.0", "content-type: text/html"]) # body_of_email can be plaintext or html! content = headers + "\r\n\r\n" + "body_of_email" session.sendmail(' youremail@gmail.com ', ' contactemail@gmail.com ', content) 

just remember, if your letter is gmail after the first start, you get an error message. then you must log in to your email account and approve access to your account from another application (you will receive a message after logging in)

+3
source

You can use a free email API such as mailgun :

 import requests def send_simple_message(target): return requests.post( "https://api.mailgun.net/v3/samples.mailgun.org/messages", auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"), data={"from": "Excited User < excited@samples.mailgun.org >", "to": [target], "subject": "Hello", "text": "Testing some Mailgun awesomeness!"}) send_simple_message(' target@email.com ') 

Using an API like this avoids the problem of individually authenticating your account.

See also: This question is for information on using smtplib

+2
source

Yes, like the answer, this is an authentication question :)

I would also like to help you send emails by reporting the yagmail package (I'm a supporter, sorry for the ad, but I feel this can really help!). Please note that I also maintain a list of common errors, such as authentication failure.

All code for you:

 import yagmail yag = yagmail.SMTP('user', 'pw') yag.send(contents = msg) 

Please note that I provide default values ​​for all arguments, for example, if you want to send yourself, you can omit " to = myemail@gmail.com ", if you do not want a subject, you can also omit it.

In addition, the goal is also to make it very easy to attach html code or images (and other files).

When you put the content, you can do something like:

 contents = ['Body text, and here is an embedded image:', 'http://somedomain/image.png', 'You can also find an audio file attached.', '/local/path/song.mp3'] 

Wow, how easy it is to send attachments! It takes 20 lines without yagmail;)

In addition, if you install it once, you no longer have to enter a password (and store it safely). In your case, you can do something like:

 import yagmail yagmail.SMTP().send(contents = contents) 

which is much more concise!

I invite you to take a look at github or install it directly using pip install yagmail .

+1
source

Source: https://habr.com/ru/post/988231/


All Articles