Python Forum
sending email with python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: sending email with python (/thread-250.html)



sending email with python - shahpy - Oct-02-2016

hi

i want to send an email from my python program

i had write this code for doing that:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
print("1")
server = smtplib.SMTP('smtp.mail.yahoo.com',465) #smtp,port number
print("2")
server.ehlo()
server.starttls()
server.ehlo()
server.login("[email protected]","my password")


 
fromaddr = "[email protected]"
toaddr = "[email protected]"
subject = "From Python"
 
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
print("3") 
body = "Sent from Python"
msg.attach(MIMEText(body, 'plain'))
 
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print('ok')
but it doesnt work.
can you help me.
myself think that my mistake is in line 5.
thank you


RE: sending email with python - snippsat - Oct-02-2016

It should work.
I replaced to gmail because i don't have yahoo.
server = smtplib.SMTP('smtp.gmail.com', 587)
Then just copy in address and password,no change to code.
Send and it work.

Check that yahoo can revive from 3 party service like this.
Remember there where i option i had to turn on to get gmail receive from all sources(allow less secure apps).


RE: sending email with python - shahpy - Oct-03-2016

thank you snippsat

i worked for myself by my gmail account

but i,m still thinking about sending email from my yahoo account


RE: sending email with python - snippsat - Oct-03-2016

Try:
server = smtplib.SMTP("smtp.mail.yahoo.com", 587)
For port 465 i think you need to change to this.
server = smtplib.SMTP_SSL('smtp.mail.yahoo.com', 465)
This create a SSL connection right from the start.
The difference is method .SMTP_SSL() which is used with 465.


RE: sending email with python - shahpy - Oct-03-2016

thank you

it worked with your help:
server = smtplib.SMTP("smtp.mail.yahoo.com", 587)