This command performs an entire mail transaction.
The arguments are: - fromaddr : The address sending this mail. - toaddrs : A list of addresses to send this mail to. A bare string will be treated as a list with 1 address. - msg : The message to send. - mailoptions : List of ESMTP options (such as 8bitmime) for the mail command. - rcptoptions : List of ESMTP options (such as DSN commands) for all the rcpt commands.
msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters.
If there has been no previous EHLO or HELO command this session, this method tries ESMTP EHLO first. If the server does ESMTP, message size and each of the specified options will be passed to it. If EHLO fails, HELO will be tried and ESMTP options suppressed.
This method will return normally if the mail is accepted for at least one recipient. It returns a dictionary, with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to the helo greeting. SMTPRecipientsRefused The server rejected ALL recipients (no mail was sent). SMTPSenderRefused The server didn't accept the fromaddr. SMTPDataError The server replied with an unexpected error code (other than a refusal of a recipient). SMTPNotSupportedError The mailoptions parameter includes 'SMTPUTF8' but the SMTPUTF8 extension is not supported by the server.
Note: the connection will be open even after an exception is raised.
Example:
import smtplib s=smtplib.SMTP("localhost") tolist=["one@one.org","two@two.org","three@three.org","four@four.org"] msg = '''\ ... From: Me@my.org ... Subject: testin'... ... ... This is a test ''' s.sendmail("me@my.org",tolist,msg) { "three@three.org" : ( 550 ,"User unknown" ) } s.quit()
In the above example, the message was accepted for delivery to three of the four addresses, and one was rejected, with the error code