Crash when sending email in Python 2.7.10
<p>I'm trying to send an email within python, but the program is crashing when I run it either as a function in a larger program or on it's own in the interpreter.</p>

<pre><code>import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

fromaddr = "exampleg@gmail.com"
toaddr = "recipient@address.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Hi there"

body = "example"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "Password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
</code></pre>

<p>In the interpreter, it seems to fail with <code>server = smtplib.SMTP('smtp.gmail.com', 587)</code></p>

<p>Any ideas?</p>

<p>My standard suggestion (as I'm the developer of it) is <a href="https://github.com/kootenpv/yagmail" rel="nofollow">yagmail</a>.</p>

<p>Install: <code>pip install yagmail</code></p>

<p>Then:</p>

<pre><code>import yagmail
yag = yagmail.SMTP(fromaddr, "pasword")
yag.send(toaddr, "Hi there", "example")
</code></pre>

<p>A lot of things can be made easier using the package, such as HTML email, adding attachments and avoiding having to write passwords in your script.</p>

<p>For the instructions to all of this (and more, sorry for the cliches), have a look at the <a href="https://github.com/kootenpv/yagmail" rel="nofollow">readme on github</a>.</p>

python email smtp