6.15. Sending e-mail

6.15.1 SendMail()

Karrigell provides a class, SendMail(), allowing programmer to easily send e-mails.

SendMail() use is :
import k_email
msg = k_email.SendMail(u"Subject of this message", 'smtp.server.com')



SendMail() parameters are :
  • subject : Title of the message.
  • smtp_server : SMTP server address used for sending the message.

6.15.2 set_from()

Method set_from(), sets the "from" field of the message.

set_from() use is :
msg.set_from("Pierre", "pierre.quentel@wanadoo.fr")


set_from() parameters are :
  • name : Name in clear form. If you don't want to give a name, name must be an empty string.
  • address : e-mail address (used for reply if no "reply to" field specified)

6.15.3 set_reply_to()

Method set_reply_to(), sets the "reply to" field of the message.

set_reply_to() use is :
msg.set_reply_to("Pierre", "pierre.quentel@wanadoo.fr")


set_reply_to() parameters are :
  • name : Name in clear form. If you don't want to give a name, name must be an empty string.
  • address : e-mail address used for reply.
If no "reply to" field is specified, the "from" address willl be used for the reply.

6.15.4 set_message()

Method set_message(), sets the content of the mesage.

set_message() use is :
msg.set_message("Content of the message.")


set_message() parameter is :
  • text : Content of the message.

6.15.5 add_recipient()

Method add_recipient(), adds a recipient.

add_recipient() use are :
msg.add_recipient("Paul", "Paul@wanadoo.fr")
msg.add_recipient("Paulette", "Paulette@wanado.fr", cc=True)



add_recipient() parameters are :
  • name : Name of the recipient in clear form. If you don't want to give a name, name must be an empty string.
  • address : e-mail address of the recipient.
  • add_to_header : If True, name and address are added to the "to" list of recipients (default value = True).
  • add_to_smtp : If True, address is added to smtp recipient list (default value = True).
  • cc : If True, name and address are added to the "cc" list of recipients (default value = False).
Only addresses added to smtp recipient list will receive the message.
This allows you to send messages to a list without reveiling the effective recipients :
msg.add_recipient("My mailing list", '', True, False) # Add name of recipient (shown in e-mailer)
msg.add_recipient("", "Paul@wanado.fr", False, True) # Add address to smtp recipient list (not shown is e-mailer)
msg.add_recipient("", "Paulette@wanado.fr", False, True) # Add address to smtp recipient list (not shown is e-mailer)
msg.add_recipient("", "Paulo@wanado.fr", False, True) # Add address to smtp recipient list (not shown is e-mailer)


6.15.6 add_attachment()

Method add_attachment(), adds an attached file to the message.

add_attachment() use is :
msg.add_attachment("My file.pdf")


add_attachment() parameter is :
  • fileName : Name of the file to add.

6.15.7 set_smtp_username_and_password()

Method set_smtp_username_and_password(), sets the login and password of SMTP server.

set_smtp_username_and_password() use is :
msg.set_smtp_username_and_password("my_name", "my_password)


set_smtp_username_and_password() parameters are :
  • smtp_user_name : User name for SMTP server login.
  • smtp_password : Password associated with user name.
Call to this method is not mandatory. You have to call set_smtp_username_and_password() only when the SMTP server you use requests authentication.

6.15.8 send()

Method send(), sends the message to all recipients that are in smtp recipient list.

send() use is :
msg.send()

6.15.9 Miscellaneous

Here is a list of other methods that can be usefull :
  • set_subject() : Replaces the title of the message.
  • message_prepend() : Adds text to the beginning of the message.
  • message_append() : Adds text to the end of the message.
  • message_prepend_file() : Adds content of a text file to the beginning of the message.
  • message_append_file() : Adds content of a text file to the end of the message.
  • clear_recipients() : Clears recipient list.
  • clear_attachments() : Clears attachment list.
  • set_smtp_server() : Replaces smtp server address.

6.15.10 Examples

In the following example, a message is sent to 4 recipients. One recipient is in the 'cc' list. Another recipient has no 'name', only its address is known. 2 files are attached to the message.

import k_email
msg = k_email.SendMail(u"Subject of this message", 'smtp.server.com')
msg.set_from (u"Karrigell WEB site", '')
msg.set_message('Hello message')
msg.set_reply_to(u"karrigell group", 'karrigell@googlegroups.com') # Not mandatory
msg.add_recipient(u'recipient 1', 'recipient1@server1.com')
msg.add_recipient(u'recipient 2', 'recipient2@server2.com')
msg.add_recipient(u'', 'recipient3@server3.com')
msg.add_recipient(u'recipient 4', 'recipient4@server4.com', cc=True) # Not mandatory
msg.add_attachment ("ASCII characters.pdf") # Not mandatory
msg.add_attachment (u"caractères UNICODE.pdf") # Not mandatory
msg.send()

In the following example, a message is sent to 3 recipients. All recipients will see 'List group' as the sender and 'Karrigell list' as recipient.

import k_email
msg = k_email.SendMail(u"Subject of this message", 'smtp.server.com')
msg.set_from (u"List group", '')
msg.set_message('Hello to list')
msg.set_reply_to(u"List group", 'list@server.com')
msg.add_recipient(u'Karrigell list', '', True, False)
msg.add_recipient(u'', 'recipient1@server1.com', False, True)
msg.add_recipient(u'', 'recipient2@server2.com', False, True)
msg.add_recipient(u'', 'recipient3@server3.com', False, True)
msg.send()