Debian Codegraph @

examples/smtp.py

raw
1 # Based on the simple libcurl SMTP example:
2 # https://github.com/bagder/curl/blob/master/docs/examples/smtp-mail.c
3 # There are other SMTP examples in that directory that you may find helpful.
4
5 from . import localhost
6 import pycurl
7 from io import BytesIO
8
9 mail_server = 'smtp://%s' % localhost
10 mail_from = '[email protected]'
11 mail_to = '[email protected]'
12
13 c = pycurl.Curl()
14 c.setopt(c.URL, mail_server)
15 c.setopt(c.MAIL_FROM, mail_from)
16 c.setopt(c.MAIL_RCPT, [mail_to])
17
18 message = '''\
19 From: %s
20 To: %s
21 Subject: PycURL SMTP example
22
23 SMTP example via PycURL
24 ''' % (mail_from, mail_to)
25
26 message = message.encode('ascii')
27
28 # libcurl does not perform buffering, therefore
29 # we need to wrap the message string into a BytesIO.
30 io = BytesIO(message)
31 c.setopt(c.READDATA, io)
32
33 # If UPLOAD is not set, libcurl performs SMTP VRFY.
34 c.setopt(c.UPLOAD, 1)
35
36 c.perform()
37 c.close()