gramex.services.emailer
¶
SMTPMailer(type, email=None, password=None, host=None, port=None, tls=True, stub=None)
¶
Creates an object capable of sending HTML emails.
Examples:
>>> mailer = SMTPMailer(type='gmail', email='gramex.guide@gmail.com', password='...')
>>> mailer.mail(
... to='person@example.com',
... subject='Subject',
... html='<strong>Bold text</strong>. <img src="cid:logo">'
... body='This plain text is shown if the client cannot render HTML',
... attachments=['1.pdf', '2.txt'],
... images={'logo': '/path/to/logo.png'})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type |
str
|
Email service type |
required |
email |
str
|
SMTP server login email ID |
None
|
password |
str
|
SMTP server login email password |
None
|
host |
str
|
SMTP server. Not required when a |
None
|
port |
int
|
SMTP server port. Defaults to 25 for non-TLS, 587 for TLS |
None
|
tls |
bool
|
True to use TLS, False to use non-TLS |
True
|
stub |
str
|
‘log’ prints email contents instead of sending it |
None
|
type
can be:
gmail
: Google Mail (smtp.gmail.com)yahoo
: Yahoo Mail (smtp.mail.yahoo.com)live
: Live.com mail (smtp.live.com)mandrill
: Mandrill (smtp.mandrillapp.com)office365
: Office 365 (smtp.office365.com)outlook
: Outlook (smtp-mail.outlook.com)icloud
: Apple iCloud (smtp.mail.me.com)mail
.com: Mail.com (smtp.mail.com)smtp
: Use ANY SMTP server viahost=
. tls=False by defaultsmtps
: Use ANY SMTP server viahost=
. tls=True by default
To test emails without sending them, use stub=True
option. This queues email info into
SMTPStub.stubs
without sending it. Use stub='log'
to print the email contents as well.
Source code in gramex\services\emailer.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
mail(kwargs)
¶
Sends an email.
Examples:
>>> mailer = SMTPMailer(type='gmail', email='gramex.guide@gmail.com', password='...')
>>> mailer.mail(
... to='person@example.com',
... subject='Subject',
... html='<strong>Bold text</strong>. <img src="cid:logo">'
... body='This plain text is shown if the client cannot render HTML',
... attachments=['1.pdf', '2.txt'],
... images={'logo': '/path/to/logo.png'})
>>> message(to='b@example.org', subject=sub, body=text, html=html)
>>> message(to='b@example.org', subject=sub, body=text, attachments=['file.pdf'])
>>> message(to='b@example.org', subject=sub, body=text, attachments=[
{'filename': 'test.txt', 'body': 'File contents'}
])
>>> message(to='b@example.org', subject=sub, html='<img src="cid:logo">',
images={'logo': 'd:/images/logo.png'})
Parameters may be any email parameter in RFC 2822. Parameters are case insensitive. Most commonly used are:
to
: The recipient email addresscc
: The carbon copy recipient email addressbcc
: The blind carbon copy recipient email addressreply_to
: The reply-to email addresson_behalf_of
: The sender email addresssubject
: The email subjectbody
: text content of the emailhtml
: HTML content of the email. If bothhtml
andbody
are specified, the email contains both parts. Email clients may decide to show one or the other.attachments
: an list of file names or dict with:body
: a byte array of the contentcontent_type
: MIME type orfilename
indicating the file name
images
: dict of{key: path}
.key
may be anything. The HTML should include the image via<img src="cid:key">
path
is the absolute path to the image
In addition, any keyword arguments passed are treated as message headers.
To
, Cc
and Bcc
may be:
- a string with comma-separated emails, e.g.
'a@x.com, b@x.com'
- a list of strings with emails, e.g.
['a@x.com', 'b@x.com']
- a list of strings with comma-separated emails, e.g.
['a@x.com', 'b@x.com, c@x.com']
Source code in gramex\services\emailer.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
mail_log(kwargs)
¶
Same as mail() but logs the exception. Useful with running in a thread
Source code in gramex\services\emailer.py
169 170 171 172 173 174 175 |
|