send_mail(payload,
mail_from,
rcpt_to,
smtp_host,
smtp_port=25,
smtp_mode=' normal ' ,
smtp_login=None,
smtp_password=None)
| source code
|
Send the message to a SMTP host. Handle SSL, TLS and authentication.
payload, mail_from and rcpt_to can come from values
returned by complete_mail(). This function call send_mail2() but catch all exceptions and return
friendly error message instead.
- Parameters:
payload (str) - the mail content.
mail_from (str) - the sender address, for example: 'me@domain.com' .
rcpt_to (list) - The list of the recipient addresses in the form [
'a@b.com', c@d.com', ] . No names here, only email
addresses.
smtp_host (str) - the IP address or the name of the SMTP host.
smtp_port (int) - the port to connect to on the SMTP host. Default is
25 .
smtp_mode (str) - the way to connect to the SMTP host, can be:
'normal' , 'ssl' or 'tls' .
default is 'normal'
smtp_login (str or None) - If authentication is required, this is the login. Be carefull to
UTF8 encode your login if it contains non us-ascii
characters.
smtp_password (str or None) - If authentication is required, this is the password. Be carefull
to UTF8 encode your password if it contains non
us-ascii characters.
- Returns: dict or str
- This function return a dictionary of failed recipients or a
string with an error message.
If all recipients have been accepted the dictionary is empty.
If the returned value is a string, none of the recipients will
get the message.
The dictionary is exactly of the same sort as
smtplib.SMTP.sendmail() returns 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.
Example:
>>> send_mail('Subject: hello\n\nmessage', 'a@foo.com', [ 'b@bar.com', ], 'localhost')
{}
Here is how to use the returned value:
if isinstance(ret, dict):
if ret:
print 'failed' recipients:
for recipient, (code, msg) in ret.iteritems():
print 'code=%d recipient=%s error=%s' % (code, recipient, msg)
else:
print 'success'
else:
print 'Error:', ret
To use your GMail account to send your mail:
smtp_host='smtp.gmail.com'
smtp_port=587
smtp_mode='tls'
smtp_login='your.gmail.addresse@gmail.com'
smtp_password='your.gmail.password'
Use your GMail address for the sender !
|