mirror of
https://github.com/AdaCore/git-hooks.git
synced 2026-02-12 12:43:11 -08:00
In preparation for the transition to Python 3, convert to using the
print function. The special import
from __future__ import print_function
in Python 2.7 makes "print" become the function and not the statement.
In Python 3, it has no effect. Once the transition is done and we
exclusively use Python 3, we can simply remove the imports.
The testsuite shows no regressions.
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
"""A module to send emails...
|
|
"""
|
|
from __future__ import print_function
|
|
import os
|
|
from subprocess import Popen, PIPE, STDOUT
|
|
|
|
|
|
def sendmail(from_email, to_emails, mail_as_string, smtp_server):
|
|
"""Send an email with sendmail or stmplib
|
|
|
|
PARAMETERS
|
|
from_email: the address sending this email (e.g. user@example.com)
|
|
to_emails: A list of addresses to send this email to.
|
|
mail_as_string: the message to send (with headers)
|
|
|
|
RETURNS
|
|
A boolean (sent / not sent)
|
|
|
|
REMARKS
|
|
We prefer running sendmail over using smtplib because
|
|
sendmail queues the email and retries a few times if
|
|
the target server is unable to receive the email.
|
|
"""
|
|
for sendmail in ('/usr/lib/sendmail', '/usr/sbin/sendmail'):
|
|
if os.path.exists(sendmail):
|
|
p = Popen([sendmail] + to_emails,
|
|
stdin=PIPE, stdout=PIPE, stderr=STDOUT)
|
|
out, _ = p.communicate(mail_as_string)
|
|
if p.returncode != 0:
|
|
print(out)
|
|
return p.returncode == 0
|
|
|
|
# Else try using smtplib
|
|
import smtplib
|
|
s = smtplib.SMTP(smtp_server)
|
|
s.sendmail(from_email, to_emails, mail_as_string)
|
|
s.quit()
|
|
return True
|