1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
3 # This file is part of pgp-mime.
5 # pgp-mime is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
10 # pgp-mime is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # pgp-mime. If not, see <http://www.gnu.org/licenses/>.
17 import configparser as _configparser
18 import smtplib as _smtplib
20 from . import LOG as _LOG
23 SENDMAIL = ['/usr/sbin/sendmail', '-t']
26 def get_smtp_params(config):
27 r"""Retrieve SMTP paramters from a config file.
29 >>> from configparser import ConfigParser
30 >>> config = ConfigParser()
31 >>> config.read_string('\n'.join([
33 ... 'host: smtp.mail.uu.edu',
36 ... 'username: rincewind',
37 ... 'password: 7ugg@g3',
39 >>> get_smtp_params(config)
40 ('smtp.mail.uu.edu', 587, True, 'rincewind', '7ugg@g3')
41 >>> config = ConfigParser()
42 >>> get_smtp_params(ConfigParser())
43 (None, None, None, None, None)
46 host = config.get('smtp', 'host')
47 except _configparser.NoSectionError:
48 return (None, None, None, None, None)
49 except _configparser.NoOptionError:
52 port = config.getint('smtp', 'port')
53 except _configparser.NoOptionError:
56 starttls = config.getboolean('smtp', 'starttls')
57 except _configparser.NoOptionError:
60 username = config.get('smtp', 'username')
61 except _configparser.NoOptionError:
64 password = config.get('smtp', 'password')
65 except _configparser.NoOptionError:
67 return (host, port, starttls, username, password)
69 def get_smtp(host=None, port=None, starttls=None, username=None,
71 """Connect to an SMTP host using the given parameters.
74 >>> try: # doctest: +SKIP
75 ... smtp = get_smtp(host='smtp.gmail.com', port=587, starttls=True,
76 ... username='rincewind@uu.edu', password='7ugg@g3')
77 ... except smtplib.SMTPAuthenticationError as error:
78 ... print('that was not a real account')
79 that was not a real account
80 >>> smtp = get_smtp() # doctest: +SKIP
81 >>> smtp.quit() # doctest: +SKIP
86 port = _smtplib.SMTP_PORT
87 if username and not starttls:
89 'sending passwords in the clear is unsafe! Use STARTTLS.')
90 _LOG.info('connect to SMTP server at {}:{}'.format(host, port))
91 smtp = _smtplib.SMTP(host=host, port=port)
96 smtp.login(username, password)
97 #smtp.set_debuglevel(1)
100 def mail(message, smtp=None, sendmail=None):
101 """Send an email ``Message`` instance on its merry way.
103 We can shell out to the user specified sendmail in case
104 the local host doesn't have an SMTP server set up
105 for easy ``smtplib`` usage.
107 >>> from pgp_mime.email import encodedMIMEText
108 >>> message = encodedMIMEText('howdy!')
109 >>> message['From'] = 'John Doe <jdoe@a.gov.ru>'
110 >>> message['To'] = 'Jack <jack@hill.org>, Jill <jill@hill.org>'
111 >>> mail(message=message, sendmail=SENDMAIL) # doctest: +SKIP
113 _LOG.info('send message {} -> {}'.format(message['from'], message['to']))
115 smtp.send_message(msg=message)
118 sendmail, stdin=message.as_string().encode('us-ascii'),
121 smtp = _smtplib.SMTP()
123 smtp.send_message(msg=message)