smtp Module

This module contains the Smtp class which handles incoming SMTP connections

class pymail.smtp.Smtp(server_, listener, config)

Bases: object

Handle incoming SMTP protocol

__init__(server_, listener, config)

Create a Smtp object

Parameters:
  • listener (socket) – socket to listen on
  • server (Server) – server object
  • config (dict) – config name in servers section
address = None

IP

advertisedAddress = None

Advertised server address (domain or IP)

authenticate(username, password)

Set authentication credentials

Parameters:
  • username (string) –
  • password (string) –

Searches all authorizers for one that authenticates against the given username/password. If one is found, The username is set and 235 is sent back. If none is found, 535 is send back. This is usually called from an authenticator object.

authenticationState(line)

Handle line in authentication state

Parameters:line
authenticator = None

Authenticator currently in use

authenticators = {'PLAIN': <class 'pymail.authenticator.PlainAuthenticator'>}

Registry of authenticator classes

authorizers = {'users': <class 'pymail.authorisation.ConfigAuthorisation'>}

Registry of authorizers

buffer = None

Body buffer

client = None

Client address

commandState(line)

Handle line in command state

Parameters:line (string) – input line

In command state, lines are interpreted as SMTP commands. The line is split into words separated by white space. If a method named handle<command> (where command is the title case form of the input command) exists, it is called with the list of words. If none exists, a 500 error is sent back.

config = None

Config section (within servers)

dataState(line)

Handle line in DATA state

Parameters:line (string) – input line

In DATA state, all lines are interpreted as data upto a single line consisting of a period character which will switch to COMMAND state.

description = None

Server description

handleAuth(words)

Handle AUTH command

handleData(words)

Handle DATA

Parameters:words (string-list) – command and arguments

A Job row is written to the database

handleEhlo(words)

Handle HELO <domain>

Parameters:words (string-list) – command and arguments
handleHelo(words)

Handle HELO <domain>

Parameters:words (string-list) – command and arguments
handleHelp(_words)

Handle HELP command

Parameters:_words (string) – unused
handleMail(words)

Handle MAIL FROM:<source>

Parameters:words (string-list) – command and arguments
handleNoop(_words)

Handle NOOP command

Parameters:_words (string) – unused
handleQuit(words)

Handle QUIT command

Parameters:words (string-list) – command and arguments
handleRcpt(words)

Handle RCPT TO:<destination>

Parameters:words (string-list) – command and arguments
handleRset(_words)

Handle RSET command

Parameters:_words (string) – unused
handleStarttls(words)

Handle STARTTLS command

Parameters:words (string) –
headerState(line)

Handle line in HEADER state

Parameters:line (string) – input line

In HEADER state, lines are interpreted as mail headers upto a empty line. An empty line will switch to the DATA state. This will also cause a record to be written into the Job table.

headers = None

List of headers from mail

inputLine()

Read line from socket

Returns:line from socket
job = None

Job object

jobId = None

Job ID (DB row ID)

listener = None

Listener

log = None

Logger

mailAddressRe = <_sre.SRE_Pattern object at 0x247ee80>

Regular expression for parsing mail addresses like <j.doe@somewhere.net> The regex is used in MAIL FROM:<...> and RCPT TO:<...> commands. The address is made a group for extraction.

mailFrom = None

Sender domain

messageId = None

Message ID

messages = {530: 'Authentication required', 535: 'Authentication failed', 421: '%s Service not available, closing transmission channel', 550: 'Requested action not taken: mailbox unavailable', 551: 'User not local; please try %s', 552: 'Requested mail action aborted: exceeded storage allocation', 553: 'Requested action not taken: mailbox name not allowed', 554: 'Transaction failed', 450: 'Requested mail action not taken: mailbox unavailable', 451: 'Requested action aborted: local error in processing', 452: 'Requested action not taken: insufficient system storage', 214: 'You are on your own', 220: '%s %s ESMTP Service ready', 221: '%s Service closing transmission channel', 354: 'Start spreading the news', 235: 'OK, go ahead', 500: 'Syntax error', 501: 'Syntax error in parameters or arguments', 502: 'Command not implemented', 503: 'Bad sequence of commands', 504: 'Command parameter not implemented', 250: '%s'}

Message catalog

rcptTo = None

Destination list

reset()

Reset transaction

Clears all data regarding the mail transfer session

send(code, *args, **kwargs)

Send line to socket

Parameters:
  • code (int) – status code
  • *args (strings) – zero or more strings to fill %-formats
  • **msg – list, tuple, or single object to send instead of message associated with code

If keyword argument msg is present, it constitutes the whole message and Arguments in *args are ignored in this case. If msg is a tuple or list, the elements are sent according to the rule of RFC 1869. If msg is not present, a text associated with the code is sent with its format codes filled in from the arguments in *args.

sendOk(msg='OK')

Send a 250 OK message

Parameters:msg (string) – optional message. Default is “OK”
server = None

Server instance

smtpMode = None

SMTP mode (‘SMTP’ (default) or ‘ESMTP’ when STARTTLS or AUTH was used)

socket = None

Network socket

sockfile = None

Network socket turned into file handle

start()

Handle a complete SMTP connection from connect to disconnect

state = None

State (method)

tlsCertificate = None

TLS certificate file

tlsEnabled = None

TLS enabled

tlsKey = None

TLS key file

tlsStart = None

STARTTLS mode (True: use STARTTLS, False: start connection in TLS)

username = None

Username

wrapper = <textwrap.TextWrapper instance at 0x24b9950>

Text wrapper: splits line after 78 characters and indents subsequent lines