Try:
The difference is method .SMTP_SSL() which is used with 465.
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.