Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ansible smtp mail add username and password #7213

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions library/notification/mail
Expand Up @@ -69,6 +69,17 @@ options:
- The body of the email being sent.
default: $subject
required: false
username:
description:
- If SMTP requires username
default: null
required: false
version_added: "1.6"
password:
- If SMTP requires password
default: null
required: false
version_added: "1.6"
host:
description:
- The mail server
Expand Down Expand Up @@ -122,6 +133,7 @@ EXAMPLES = '''
import os
import sys
import smtplib
import ssl

try:
from email import encoders
Expand All @@ -142,6 +154,8 @@ def main():

module = AnsibleModule(
argument_spec = dict(
username = dict(default=None),
password = dict(default=None),
host = dict(default='localhost'),
port = dict(default='25'),
sender = dict(default='root', aliases=['from']),
Expand All @@ -156,6 +170,8 @@ def main():
)
)

username = module.params.get('username')
password = module.params.get('password')
host = module.params.get('host')
port = module.params.get('port')
sender = module.params.get('sender')
Expand All @@ -167,17 +183,23 @@ def main():
attach_files = module.params.get('attach')
headers = module.params.get('headers')
charset = module.params.get('charset')

sender_phrase, sender_addr = parseaddr(sender)

if not body:
body = subject

try:
smtp = smtplib.SMTP_SSL(host, port=int(port))
except (smtplib.SMTPException, ssl.SSLError):
smtp = smtplib.SMTP(host, port=int(port))
except Exception, e:
module.fail_json(rc=1, msg='Failed to send mail to server %s on port %s: %s' % (host, port, e))

smtp.ehlo()
if username and password:
if smtp.has_extn('STARTTLS'):
smtp.starttls()
try:
smtp.login(username, password)
except smtplib.SMTPAuthenticationError:
module.fail_json(msg="Authentication to %s:%s failed, please check your username and/or password" % (host, port))

msg = MIMEMultipart()
msg['Subject'] = subject
Expand Down