mail: Fix regression when sending mail without SSL (#46403)

When refactoring the code the non-SSL use-case was broken.

The main cause is that we have no way to do integration tests for
testing SMTP.
pull/46548/merge
Dag Wieers 6 years ago committed by Brian Coca
parent 65952571d6
commit b82f965616

@ -36,67 +36,81 @@ options:
from: from:
description: description:
- The email-address the mail is sent from. May contain address and phrase. - The email-address the mail is sent from. May contain address and phrase.
type: str
default: root default: root
to: to:
description: description:
- The email-address(es) the mail is being sent to. - The email-address(es) the mail is being sent to.
- This is a list, which may contain address and phrase portions. - This is a list, which may contain address and phrase portions.
type: list
default: root default: root
aliases: ['recipients'] aliases: [ recipients ]
cc: cc:
description: description:
- The email-address(es) the mail is being copied to. - The email-address(es) the mail is being copied to.
- This is a list, which may contain address and phrase portions. - This is a list, which may contain address and phrase portions.
type: list
bcc: bcc:
description: description:
- The email-address(es) the mail is being 'blind' copied to. - The email-address(es) the mail is being 'blind' copied to.
- This is a list, which may contain address and phrase portions. - This is a list, which may contain address and phrase portions.
type: list
subject: subject:
description: description:
- The subject of the email being sent. - The subject of the email being sent.
required: true required: yes
type: str
body: body:
description: description:
- The body of the email being sent. - The body of the email being sent.
type: str
default: $subject default: $subject
username: username:
description: description:
- If SMTP requires username. - If SMTP requires username.
type: str
version_added: '1.9' version_added: '1.9'
password: password:
description: description:
- If SMTP requires password. - If SMTP requires password.
type: str
version_added: '1.9' version_added: '1.9'
host: host:
description: description:
- The mail server. - The mail server.
type: str
default: localhost default: localhost
port: port:
description: description:
- The mail server port. - The mail server port.
- This must be a valid integer between 1 and 65534 - This must be a valid integer between 1 and 65534
type: int
default: 25 default: 25
version_added: '1.0' version_added: '1.0'
attach: attach:
description: description:
- A list of pathnames of files to attach to the message. - A list of pathnames of files to attach to the message.
- Attached files will have their content-type set to C(application/octet-stream). - Attached files will have their content-type set to C(application/octet-stream).
type: list
default: [] default: []
version_added: '1.0' version_added: '1.0'
headers: headers:
description: description:
- A list of headers which should be added to the message. - A list of headers which should be added to the message.
- Each individual header is specified as C(header=value) (see example below). - Each individual header is specified as C(header=value) (see example below).
type: list
default: [] default: []
version_added: '1.0' version_added: '1.0'
charset: charset:
description: description:
- The character set of email being sent. - The character set of email being sent.
type: str
default: utf-8 default: utf-8
subtype: subtype:
description: description:
- The minor mime type, can be either C(plain) or C(html). - The minor mime type, can be either C(plain) or C(html).
- The major type is always C(text). - The major type is always C(text).
type: str
choices: [ html, plain ] choices: [ html, plain ]
default: plain default: plain
version_added: '2.0' version_added: '2.0'
@ -108,12 +122,14 @@ options:
- If C(never), the connection will not attempt to setup a secure SSL/TLS session, before sending - If C(never), the connection will not attempt to setup a secure SSL/TLS session, before sending
- If C(starttls), the connection will try to upgrade to a secure SSL/TLS connection, before sending. - If C(starttls), the connection will try to upgrade to a secure SSL/TLS connection, before sending.
If it is unable to do so it will fail. If it is unable to do so it will fail.
type: str
choices: [ always, never, starttls, try ] choices: [ always, never, starttls, try ]
default: try default: try
version_added: '2.3' version_added: '2.3'
timeout: timeout:
description: description:
- Sets the timeout in seconds for connection attempts. - Sets the timeout in seconds for connection attempts.
type: int
default: 20 default: 20
version_added: '2.3' version_added: '2.3'
''' '''
@ -255,6 +271,8 @@ def main():
if secure == 'always': if secure == 'always':
module.fail_json(rc=1, msg='Unable to start an encrypted session to %s:%s: %s' % module.fail_json(rc=1, msg='Unable to start an encrypted session to %s:%s: %s' %
(host, port, to_native(e)), exception=traceback.format_exc()) (host, port, to_native(e)), exception=traceback.format_exc())
except:
pass
if not secure_state: if not secure_state:
smtp = smtplib.SMTP(timeout=timeout) smtp = smtplib.SMTP(timeout=timeout)

Loading…
Cancel
Save