| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Matt Makai <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: sendgrid |
| 11 |
short_description: Sends an email with the SendGrid API |
| 12 |
description: |
| 13 |
- Sends an email with a SendGrid account through their API, not through the SMTP service. |
| 14 |
notes: |
| 15 |
- This module is non-idempotent because it sends an email through the external API. It is idempotent only in the case that |
| 16 |
the module fails. |
| 17 |
- Like the other notification modules, this one requires an external dependency to work. In this case, you need an active |
| 18 |
SendGrid account. |
| 19 |
- In order to use O(api_key), O(cc), O(bcc), O(attachments), O(from_name), O(html_body), and O(headers) you must C(pip install |
| 20 |
sendgrid). |
| 21 |
requirements: |
| 22 |
- sendgrid Python library 1.6.22 or lower (Sendgrid API V2 supported) |
| 23 |
extends_documentation_fragment: |
| 24 |
- community.general._attributes |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
options: |
| 31 |
username: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- Username for logging into the SendGrid account. |
| 35 |
- It is only required if O(api_key) is not supplied. |
| 36 |
password: |
| 37 |
type: str |
| 38 |
description: |
| 39 |
- Password that corresponds to the username. |
| 40 |
- It is only required if O(api_key) is not supplied. |
| 41 |
from_address: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- The address in the "from" field for the email. |
| 45 |
required: true |
| 46 |
to_addresses: |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
description: |
| 50 |
- A list with one or more recipient email addresses. |
| 51 |
required: true |
| 52 |
subject: |
| 53 |
type: str |
| 54 |
description: |
| 55 |
- The desired subject for the email. |
| 56 |
required: true |
| 57 |
api_key: |
| 58 |
type: str |
| 59 |
description: |
| 60 |
- Sendgrid API key to use instead of username/password. |
| 61 |
cc: |
| 62 |
type: list |
| 63 |
elements: str |
| 64 |
description: |
| 65 |
- A list of email addresses to cc. |
| 66 |
bcc: |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
description: |
| 70 |
- A list of email addresses to bcc. |
| 71 |
attachments: |
| 72 |
type: list |
| 73 |
elements: path |
| 74 |
description: |
| 75 |
- A list of relative or explicit paths of files you want to attach (7MB limit as per SendGrid docs). |
| 76 |
from_name: |
| 77 |
type: str |
| 78 |
description: |
| 79 |
- The name you want to appear in the from field, for example V(John Doe). |
| 80 |
html_body: |
| 81 |
description: |
| 82 |
- Whether the body is HTML content that should be rendered. |
| 83 |
type: bool |
| 84 |
default: false |
| 85 |
headers: |
| 86 |
type: dict |
| 87 |
description: |
| 88 |
- A dict to pass on as headers. |
| 89 |
body: |
| 90 |
type: str |
| 91 |
description: |
| 92 |
- The e-mail body content. |
| 93 |
required: true |
| 94 |
author: "Matt Makai (@makaimc)" |
| 95 |
""" |
| 96 |
|
| 97 |
EXAMPLES = r""" |
| 98 |
- name: Send an email to a single recipient that the deployment was successful |
| 99 |
community.general.sendgrid: |
| 100 |
username: "{{ sendgrid_username }}" |
| 101 |
password: "{{ sendgrid_password }}" |
| 102 |
from_address: "[email protected]" |
| 103 |
to_addresses: |
| 104 |
- "[email protected]" |
| 105 |
subject: "Deployment success." |
| 106 |
body: "The most recent Ansible deployment was successful." |
| 107 |
delegate_to: localhost |
| 108 |
|
| 109 |
- name: Send an email to more than one recipient that the build failed |
| 110 |
community.general.sendgrid: |
| 111 |
username: "{{ sendgrid_username }}" |
| 112 |
password: "{{ sendgrid_password }}" |
| 113 |
from_address: "[email protected]" |
| 114 |
to_addresses: |
| 115 |
- "[email protected]" |
| 116 |
- "[email protected]" |
| 117 |
subject: "Build failure!." |
| 118 |
body: "Unable to pull source repository from Git server." |
| 119 |
delegate_to: localhost |
| 120 |
""" |
| 121 |
|
| 122 |
# ======================================= |
| 123 |
# sendgrid module support methods |
| 124 |
# |
| 125 |
import os |
| 126 |
import traceback |
| 127 |
from urllib.parse import urlencode |
| 128 |
|
| 129 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 130 |
|
| 131 |
SENDGRID_IMP_ERR = None |
| 132 |
try: |
| 133 |
import sendgrid |
| 134 |
|
| 135 |
HAS_SENDGRID = True |
| 136 |
except ImportError: |
| 137 |
SENDGRID_IMP_ERR = traceback.format_exc() |
| 138 |
HAS_SENDGRID = False |
| 139 |
|
| 140 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 141 |
from ansible.module_utils.common.text.converters import to_bytes |
| 142 |
from ansible.module_utils.urls import fetch_url |
| 143 |
|
| 144 |
|
| 145 |
def post_sendgrid_api( |
| 146 |
module, |
| 147 |
username, |
| 148 |
password, |
| 149 |
from_address, |
| 150 |
to_addresses, |
| 151 |
subject, |
| 152 |
body, |
| 153 |
api_key=None, |
| 154 |
cc=None, |
| 155 |
bcc=None, |
| 156 |
attachments=None, |
| 157 |
html_body=False, |
| 158 |
from_name=None, |
| 159 |
headers=None, |
| 160 |
): |
| 161 |
if not HAS_SENDGRID: |
| 162 |
SENDGRID_URI = "https://api.sendgrid.com/api/mail.send.json" |
| 163 |
AGENT = "Ansible" |
| 164 |
data = {"api_user": username, "api_key": password, "from": from_address, "subject": subject, "text": body} |
| 165 |
encoded_data = urlencode(data) |
| 166 |
to_addresses_api = "" |
| 167 |
for recipient in to_addresses: |
| 168 |
recipient = to_bytes(recipient, errors="surrogate_or_strict") |
| 169 |
to_addresses_api += f"&to[]={recipient}" |
| 170 |
encoded_data += to_addresses_api |
| 171 |
|
| 172 |
headers = { |
| 173 |
"User-Agent": AGENT, |
| 174 |
"Content-type": "application/x-www-form-urlencoded", |
| 175 |
"Accept": "application/json", |
| 176 |
} |
| 177 |
return fetch_url(module, SENDGRID_URI, data=encoded_data, headers=headers, method="POST") |
| 178 |
else: |
| 179 |
# Remove this check when adding Sendgrid API v3 support |
| 180 |
if LooseVersion(sendgrid.version.__version__) > LooseVersion("1.6.22"): |
| 181 |
module.fail_json(msg="Please install sendgrid==1.6.22 or lower since module uses Sendgrid V2 APIs.") |
| 182 |
|
| 183 |
if api_key: |
| 184 |
sg = sendgrid.SendGridClient(api_key) |
| 185 |
else: |
| 186 |
sg = sendgrid.SendGridClient(username, password) |
| 187 |
|
| 188 |
message = sendgrid.Mail() |
| 189 |
message.set_subject(subject) |
| 190 |
|
| 191 |
for recip in to_addresses: |
| 192 |
message.add_to(recip) |
| 193 |
|
| 194 |
if cc: |
| 195 |
for recip in cc: |
| 196 |
message.add_cc(recip) |
| 197 |
if bcc: |
| 198 |
for recip in bcc: |
| 199 |
message.add_bcc(recip) |
| 200 |
|
| 201 |
if headers: |
| 202 |
message.set_headers(headers) |
| 203 |
|
| 204 |
if attachments: |
| 205 |
for f in attachments: |
| 206 |
name = os.path.basename(f) |
| 207 |
message.add_attachment(name, f) |
| 208 |
|
| 209 |
if from_name: |
| 210 |
message.set_from(f"{from_name} <{from_address}.") |
| 211 |
else: |
| 212 |
message.set_from(from_address) |
| 213 |
|
| 214 |
if html_body: |
| 215 |
message.set_html(body) |
| 216 |
else: |
| 217 |
message.set_text(body) |
| 218 |
|
| 219 |
return sg.send(message) |
| 220 |
|
| 221 |
|
| 222 |
# ======================================= |
| 223 |
# Main |
| 224 |
# |
| 225 |
|
| 226 |
|
| 227 |
def main(): |
| 228 |
module = AnsibleModule( |
| 229 |
argument_spec=dict( |
| 230 |
username=dict(), |
| 231 |
password=dict(no_log=True), |
| 232 |
api_key=dict(no_log=True), |
| 233 |
bcc=dict(type="list", elements="str"), |
| 234 |
cc=dict(type="list", elements="str"), |
| 235 |
headers=dict(type="dict"), |
| 236 |
from_address=dict(required=True), |
| 237 |
from_name=dict(), |
| 238 |
to_addresses=dict(required=True, type="list", elements="str"), |
| 239 |
subject=dict(required=True), |
| 240 |
body=dict(required=True), |
| 241 |
html_body=dict(default=False, type="bool"), |
| 242 |
attachments=dict(type="list", elements="path"), |
| 243 |
), |
| 244 |
supports_check_mode=True, |
| 245 |
mutually_exclusive=[["api_key", "password"], ["api_key", "username"]], |
| 246 |
required_together=[["username", "password"]], |
| 247 |
) |
| 248 |
|
| 249 |
username = module.params["username"] |
| 250 |
password = module.params["password"] |
| 251 |
api_key = module.params["api_key"] |
| 252 |
bcc = module.params["bcc"] |
| 253 |
cc = module.params["cc"] |
| 254 |
headers = module.params["headers"] |
| 255 |
from_name = module.params["from_name"] |
| 256 |
from_address = module.params["from_address"] |
| 257 |
to_addresses = module.params["to_addresses"] |
| 258 |
subject = module.params["subject"] |
| 259 |
body = module.params["body"] |
| 260 |
html_body = module.params["html_body"] |
| 261 |
attachments = module.params["attachments"] |
| 262 |
|
| 263 |
sendgrid_lib_args = [api_key, bcc, cc, headers, from_name, html_body, attachments] |
| 264 |
|
| 265 |
if any(lib_arg is not None for lib_arg in sendgrid_lib_args) and not HAS_SENDGRID: |
| 266 |
reason = ( |
| 267 |
"when using any of the following arguments: api_key, bcc, cc, headers, from_name, html_body, attachments" |
| 268 |
) |
| 269 |
module.fail_json(msg=missing_required_lib("sendgrid", reason=reason), exception=SENDGRID_IMP_ERR) |
| 270 |
|
| 271 |
response, info = post_sendgrid_api( |
| 272 |
module, |
| 273 |
username, |
| 274 |
password, |
| 275 |
from_address, |
| 276 |
to_addresses, |
| 277 |
subject, |
| 278 |
body, |
| 279 |
attachments=attachments, |
| 280 |
bcc=bcc, |
| 281 |
cc=cc, |
| 282 |
headers=headers, |
| 283 |
html_body=html_body, |
| 284 |
api_key=api_key, |
| 285 |
) |
| 286 |
|
| 287 |
if not HAS_SENDGRID: |
| 288 |
if info["status"] != 200: |
| 289 |
module.fail_json(msg=f"unable to send email through SendGrid API: {info['msg']}") |
| 290 |
else: |
| 291 |
if response != 200: |
| 292 |
module.fail_json(msg=f"unable to send email through SendGrid API: {info['message']}") |
| 293 |
|
| 294 |
module.exit_json(msg=subject, changed=False) |
| 295 |
|
| 296 |
|
| 297 |
if __name__ == "__main__": |
| 298 |
main() |