09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 13 ■ SMTP<br />

ETRN<br />

STARTTLS<br />

XVERP<br />

8BITMIME<br />

send: 'mail FROM: size=157\r\n'<br />

reply: '250 Ok\r\n'<br />

reply: retcode (250); Msg: Ok<br />

send: 'rcpt TO:\r\n'<br />

reply: '250 Ok\r\n'<br />

reply: retcode (250); Msg: Ok<br />

send: 'data\r\n'<br />

reply: '354 End data with .\r\n'<br />

reply: retcode (354); Msg: End data with .<br />

data: (354, 'End data with .')<br />

send: 'To: jgoerzen@complete.org\r\n<br />

From: foo@example.com\r\n<br />

Subject: Test Message from simple.py\r\n<br />

\r\n<br />

Hello,\r\n<br />

\r\n<br />

This is a test message sent to you from simple.py and smtplib.<br />

\r\n.<br />

\r\n'<br />

reply: '250 Ok: queued as 8094C18C0\r\n'<br />

reply: retcode (250); Msg: Ok: queued as 8094C18C0<br />

data: (250, 'Ok: queued as 8094C18C0')<br />

Message successfully sent to 1 recipient(s)<br />

From this example, you can see the conversation that smtplib is having with the SMTP server over<br />

the network. As you implement code that uses more advanced SMTP features, the details shown here<br />

will be more important, so let’s look at what’s happening.<br />

First, the client (the smtplib library) sends an EHLO command (an “extended” successor to a more<br />

ancient command that was named, more readably, HELO) with your hostname in it. The remote server<br />

responds with its hostname, and lists any optional SMTP features that it supports.<br />

Next, the client sends the mail from command, which states the “envelope sender” e-mail address<br />

and the size <strong>of</strong> the message. The server at this moment has the opportunity to reject the message (for<br />

example, because it thinks you are a spammer); but in this case, it responds with 250 Ok. (Note that in<br />

this case, the code 250 is what matters; the remaining text is just a human-readable comment and varies<br />

from server to server.)<br />

Then the client sends a rcpt to command, with the “envelope recipient” that we talked so much<br />

about earlier in this chapter; you can finally see that, indeed, it is transmitted separately from the text <strong>of</strong><br />

the message itself when using the SMTP protocol. If you were sending the message to more than one<br />

recipient, they would each be listed on the rcpt to line.<br />

Finally, the client sends a data command, transmits the actual message (using verbose carriagereturn-linefeed<br />

line endings, you will note, per the Internet e-mail standard), and finishes the<br />

conversation.<br />

The smtplib module is doing all this automatically for you in this example. In the rest <strong>of</strong> the chapter,<br />

we will look at how to take more control <strong>of</strong> the process so you can take advantage <strong>of</strong> some more<br />

advanced features.<br />

227

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!