12

I have yahoo account. Is there any python code to send email from my account ?

user2351194
  • 157
  • 1
  • 1
  • 8

6 Answers6

17

Yes, here is the code :

import smtplib
fromMy = 'yourMail@yahoo.com' # fun-fact: "from" is a keyword in python, you can't use it as variable.. did anyone check if this code even works?
to  = 'SomeOne@Example.com'
subj='TheSubject'
date='2/1/2010'
message_text='Hello Or any thing you want to send'

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )
  
username = str('yourMail@yahoo.com')  
password = str('yourPassWord')  
  
try :
    server = smtplib.SMTP("smtp.mail.yahoo.com",587)
    server.login(username,password)
    server.sendmail(fromMy, to,msg)
    server.quit()    
    print 'ok the email has sent '
except :
    print 'can\'t send the Email'
user2229472
  • 509
  • 3
  • 10
  • 5
    server.starttls() should be added before line with server.login otherwise it will throw an exception. – user6972 Feb 17 '14 at 08:39
  • 7
    `SMTP AUTH extension not supported by server.` – Volatil3 Jul 17 '17 at 06:40
  • 2
    you have to enable app authetication in your settings to do this. – Ace of Spade Nov 13 '18 at 13:27
  • 1
    @AceofSpade I just did this in account info -> account security -> "Allow apps that use less secure sign in". is that what you were referring to? – jason m May 21 '19 at 10:35
  • my "account security" page doesn't have any buttion/link to allow these apps... did they remove this? EDIT: @user6972 's comment fixes this "SMTP AUTH" error without any modifications to the yahoo account. – Grifball Jan 29 '22 at 15:44
  • The current wording on the Account Security page for app passwords is "Other ways to sign in" -> "App Password". – nmgeek Jun 19 '22 at 16:25
9

I racked my head (briefly) regarding using yahoo's smtp server. 465 just would not work. I decided to go the TLS route over port 587 and I was able to authenticate and send email.

import smtplib
from email.mime.text import MIMEText
SMTP_SERVER = "smtp.mail.yahoo.com"
SMTP_PORT = 587
SMTP_USERNAME = "username"
SMTP_PASSWORD = "password"
EMAIL_FROM = "fromaddress@yahoo.com"
EMAIL_TO = "toaddress@gmail.com"
EMAIL_SUBJECT = "REMINDER:"
co_msg = """
Hello, [username]! Just wanted to send a friendly appointment
reminder for your appointment:
[Company]
Where: [companyAddress]
Time: [appointmentTime]
Company URL: [companyUrl]
Change appointment?? Add Service??
change notification preference (text msg/email)
"""
def send_email():
    msg = MIMEText(co_msg)
    msg['Subject'] = EMAIL_SUBJECT + "Company - Service at appointmentTime"
    msg['From'] = EMAIL_FROM 
    msg['To'] = EMAIL_TO
    debuglevel = True
    mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mail.set_debuglevel(debuglevel)
    mail.starttls()
    mail.login(SMTP_USERNAME, SMTP_PASSWORD)
    mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
    mail.quit()

if __name__=='__main__':
send_email()
xpros
  • 2,166
  • 18
  • 15
  • 2
    I've got error: `smtplib.SMTPServerDisconnected: Connection unexpectedly closed` But I have solved it by going to my yahoo "Account Security" setting and check the "Allow apps that use less secure sign in" option – Arif Nazar Purwandaru Sep 17 '19 at 00:50
  • This script _also_ requires you to set up and use an app password. This is done via the Security section of your yahoo account settings (not the email settings). – nmgeek Jun 19 '22 at 16:22
5

Visit yahoo account security page here

You'll need to generate an app password - it's an option towards the bottom of the screen. Use the password Yahoo generated on this page in your script.

edubu2
  • 108
  • 1
  • 6
3

To support non-ascii characters; you could use email package:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from email.header    import Header
from email.mime.text import MIMEText
from getpass import getpass
from smtplib import SMTP_SSL

# provide credentials
login = 'you@yahoo.com'
password = getpass('Password for "%s": ' % login)

# create message
msg = MIMEText('message body…', 'plain', 'utf-8')
msg['Subject'] = Header('subject…', 'utf-8')
msg['From'] = login
msg['To'] = ', '.join([login, ])

# send it   
s = SMTP_SSL('smtp.mail.yahoo.com', timeout=10) #NOTE: no server cert. check
s.set_debuglevel(0)
try:
    s.login(login, password)
    s.sendmail(msg['From'], msg['To'], msg.as_string())
finally:
    s.quit()
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

There are a couple of issues. One is addressed by an answer already posted.

  1. Use TLS (Port 465)
  2. Make sure you have an app password. Yahoo and other email services have updated their authentication practices to limit things that can login without 2 factor authentication. If you want to authenticate with smtplib you need to create an app password here: https://login.yahoo.com/myaccount/security/app-password

If you do that then you'll be able to send emails

Dom DaFonte
  • 1,619
  • 14
  • 31
0

For A good year and a half I had the following def working fine on PC's and Pi's. I had a script emailing me every Saturday at noon as a general health check. The working part was..

def my_callback():  
    server = smtplib.SMTP('smtp.mail.yahoo.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, message)
    server.quit()

The about two weeks ago its stopped working on all my devices. Running through the script I found that the "server.starttls()" line was the source of the failure. Investigating around I came to find that reverting to port 465 and SSL, dropping the server.starttls() fixed the Issue.

def my_callback():  
    server = smtplib.SMTP_SSL('smtp.mail.yahoo.com', 465)
    server.login(username,password)
     server.sendmail(fromaddr, toaddrs, message)
    server.quit()

Anybody else have this issue? Have Yahoo changed something?