module sender.email_sender

Short summary

module pymmails.sender.email_sender

Functions to send emails

source on GitHub

Functions

function

truncated documentation

compose_email

Composes an email as a string.

create_smtp_server

Creates a SMTP server and log into it.

send_email

Sends an email as a string.

Documentation

Functions to send emails

source on GitHub

pymmails.sender.email_sender.compose_email(fr, to, subject, body_html=None, body_text=None, attachements=None, cc=None, bcc=None)

Composes an email as a string.

Parameters:
  • fr – from

  • to – receiver (or list of receivers)

  • subject – subject

  • body_text – body text

  • body_html – body html

  • cc – list of ccs

  • bcc – list of bccs

  • attachements – list of files to attach to the email

Returns:

string

If the file is a text file, the filename can be replaced by (filename, encoding). If body_html and body_text are filled, only the first one will be used.

source on GitHub

pymmails.sender.email_sender.create_smtp_server(host, username, password)

Creates a SMTP server and log into it.

Parameters:
  • host – something like smtp.gmail.com:587

  • username – username

  • login – login

Returns:

server

You should call server.quit() when the server is not used anymore.

source on GitHub

pymmails.sender.email_sender.send_email(server, fr, to, subject, body_html=None, body_text=None, attachements=None, delay_sending=False, cc=None, bcc=None)

Sends an email as a string.

Parameters:
  • server – result from function create_smtp_server

  • fr – from

  • to – destination (or list of receivers)

  • cc – cc

  • bcc – bcc

  • subject – subject

  • body_text – body text

  • body_html – body html

  • attachements – list of files to attach to the email

  • delay_sending – if True, the function returns a function which will send the mail if executed

Returns:

function (if delay_sending), return of sendmail otherwise

Send an email

from pymmails import create_smtp_server, send_email
server = create_smtp_server("gmail", "somebody", "pwd")
send_email(server, "from.sender@gmail.com",
           "to.receiver@else.com", "subject",
           attachements = [ os.path.abspath(__file__) ])
server.quit()

Gmail does not allow to send or get emails with Python

By default, a Gmail account does not enable the IMAP access. That explains why it is not possible to send or get messages from Gmail. The following page Get started with IMAP and POP3 explains how to enable that option.

source on GitHub