You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.3 KiB

  1. #!/usr/bin/env sh
  2. # support smtp
  3. # Please report bugs to https://github.com/acmesh-official/acme.sh/issues/3358
  4. # This implementation uses either curl or Python (3 or 2.7).
  5. # (See also the "mail" notify hook, which supports other ways to send mail.)
  6. # SMTP_FROM="from@example.com" # required
  7. # SMTP_TO="to@example.com" # required
  8. # SMTP_HOST="smtp.example.com" # required
  9. # SMTP_PORT="25" # defaults to 25, 465 or 587 depending on SMTP_SECURE
  10. # SMTP_SECURE="none" # one of "none", "ssl" (implicit TLS, TLS Wrapper), "tls" (explicit TLS, STARTTLS)
  11. # SMTP_USERNAME="" # set if SMTP server requires login
  12. # SMTP_PASSWORD="" # set if SMTP server requires login
  13. # SMTP_TIMEOUT="30" # seconds for SMTP operations to timeout
  14. # SMTP_BIN="/path/to/curl_or_python" # default finds first of curl, python3, or python on PATH
  15. # subject content statuscode
  16. smtp_send() {
  17. _SMTP_SUBJECT="$1"
  18. _SMTP_CONTENT="$2"
  19. # UNUSED: _statusCode="$3" # 0: success, 1: error 2($RENEW_SKIP): skipped
  20. # Load config:
  21. SMTP_FROM="${SMTP_FROM:-$(_readaccountconf_mutable SMTP_FROM)}"
  22. SMTP_TO="${SMTP_TO:-$(_readaccountconf_mutable SMTP_TO)}"
  23. SMTP_HOST="${SMTP_HOST:-$(_readaccountconf_mutable SMTP_HOST)}"
  24. SMTP_PORT="${SMTP_PORT:-$(_readaccountconf_mutable SMTP_PORT)}"
  25. SMTP_SECURE="${SMTP_SECURE:-$(_readaccountconf_mutable SMTP_SECURE)}"
  26. SMTP_USERNAME="${SMTP_USERNAME:-$(_readaccountconf_mutable SMTP_USERNAME)}"
  27. SMTP_PASSWORD="${SMTP_PASSWORD:-$(_readaccountconf_mutable SMTP_PASSWORD)}"
  28. SMTP_TIMEOUT="${SMTP_TIMEOUT:-$(_readaccountconf_mutable SMTP_TIMEOUT)}"
  29. SMTP_BIN="${SMTP_BIN:-$(_readaccountconf_mutable SMTP_BIN)}"
  30. _debug "SMTP_FROM" "$SMTP_FROM"
  31. _debug "SMTP_TO" "$SMTP_TO"
  32. _debug "SMTP_HOST" "$SMTP_HOST"
  33. _debug "SMTP_PORT" "$SMTP_PORT"
  34. _debug "SMTP_SECURE" "$SMTP_SECURE"
  35. _debug "SMTP_USERNAME" "$SMTP_USERNAME"
  36. _secure_debug "SMTP_PASSWORD" "$SMTP_PASSWORD"
  37. _debug "SMTP_TIMEOUT" "$SMTP_TIMEOUT"
  38. _debug "SMTP_BIN" "$SMTP_BIN"
  39. _debug "_SMTP_SUBJECT" "$_SMTP_SUBJECT"
  40. _debug "_SMTP_CONTENT" "$_SMTP_CONTENT"
  41. # Validate config and apply defaults:
  42. # _SMTP_* variables are the resolved (with defaults) versions of SMTP_*.
  43. # (The _SMTP_* versions will not be stored in account conf.)
  44. if [ -n "$SMTP_BIN" ] && ! _exists "$SMTP_BIN"; then
  45. _err "SMTP_BIN '$SMTP_BIN' does not exist."
  46. return 1
  47. fi
  48. _SMTP_BIN="$SMTP_BIN"
  49. if [ -z "$_SMTP_BIN" ]; then
  50. # Look for a command that can communicate with an SMTP server.
  51. # (Please don't add sendmail, ssmtp, mutt, mail, or msmtp here.
  52. # Those are already handled by the "mail" notify hook.)
  53. for cmd in curl python3 python2.7 python pypy3 pypy; do
  54. if _exists "$cmd"; then
  55. _SMTP_BIN="$cmd"
  56. break
  57. fi
  58. done
  59. if [ -z "$_SMTP_BIN" ]; then
  60. _err "The smtp notify-hook requires curl or Python, but can't find any."
  61. _err 'If you have one of them, define SMTP_BIN="/path/to/curl_or_python".'
  62. _err 'Otherwise, see if you can use the "mail" notify-hook instead.'
  63. return 1
  64. fi
  65. _debug "_SMTP_BIN" "$_SMTP_BIN"
  66. fi
  67. if [ -z "$SMTP_FROM" ]; then
  68. _err "You must define SMTP_FROM as the sender email address."
  69. return 1
  70. fi
  71. _SMTP_FROM="$SMTP_FROM"
  72. if [ -z "$SMTP_TO" ]; then
  73. _err "You must define SMTP_TO as the recipient email address."
  74. return 1
  75. fi
  76. _SMTP_TO="$SMTP_TO"
  77. if [ -z "$SMTP_HOST" ]; then
  78. _err "You must define SMTP_HOST as the SMTP server hostname."
  79. return 1
  80. fi
  81. _SMTP_HOST="$SMTP_HOST"
  82. _SMTP_SECURE="${SMTP_SECURE:-none}"
  83. case "$_SMTP_SECURE" in
  84. "none") smtp_default_port="25" ;;
  85. "ssl") smtp_default_port="465" ;;
  86. "tls") smtp_default_port="587" ;;
  87. *)
  88. _err "Invalid SMTP_SECURE='$SMTP_SECURE'. It must be 'ssl', 'tls' or 'none'."
  89. return 1
  90. ;;
  91. esac
  92. _SMTP_PORT="${SMTP_PORT:-$smtp_default_port}"
  93. if [ -z "$SMTP_PORT" ]; then
  94. _debug "_SMTP_PORT" "$_SMTP_PORT"
  95. fi
  96. _SMTP_USERNAME="$SMTP_USERNAME"
  97. _SMTP_PASSWORD="$SMTP_PASSWORD"
  98. _SMTP_TIMEOUT="${SMTP_TIMEOUT:-30}"
  99. # Run with --debug 2 (or above) to echo the transcript of the SMTP session.
  100. # Careful: this may include SMTP_PASSWORD in plaintext!
  101. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
  102. _SMTP_SHOW_TRANSCRIPT="True"
  103. else
  104. _SMTP_SHOW_TRANSCRIPT=""
  105. fi
  106. # Send the message:
  107. case "$(basename "$_SMTP_BIN")" in
  108. curl) _smtp_send=_smtp_send_curl ;;
  109. py*) _smtp_send=_smtp_send_python ;;
  110. *)
  111. _err "Can't figure out how to invoke $_SMTP_BIN."
  112. _err "Please re-run with --debug and report a bug."
  113. return 1
  114. ;;
  115. esac
  116. if ! smtp_output="$($_smtp_send)"; then
  117. _err "Error sending message with $_SMTP_BIN."
  118. _err "${smtp_output:-(No additional details; try --debug or --debug 2)}"
  119. return 1
  120. fi
  121. # Save config only if send was successful:
  122. _saveaccountconf_mutable SMTP_BIN "$SMTP_BIN"
  123. _saveaccountconf_mutable SMTP_FROM "$SMTP_FROM"
  124. _saveaccountconf_mutable SMTP_TO "$SMTP_TO"
  125. _saveaccountconf_mutable SMTP_HOST "$SMTP_HOST"
  126. _saveaccountconf_mutable SMTP_PORT "$SMTP_PORT"
  127. _saveaccountconf_mutable SMTP_SECURE "$SMTP_SECURE"
  128. _saveaccountconf_mutable SMTP_USERNAME "$SMTP_USERNAME"
  129. _saveaccountconf_mutable SMTP_PASSWORD "$SMTP_PASSWORD"
  130. _saveaccountconf_mutable SMTP_TIMEOUT "$SMTP_TIMEOUT"
  131. return 0
  132. }
  133. # Send the message via curl using _SMTP_* variables
  134. _smtp_send_curl() {
  135. # TODO: implement
  136. echo "_smtp_send_curl not implemented"
  137. return 1
  138. }
  139. # Send the message via Python using _SMTP_* variables
  140. _smtp_send_python() {
  141. _debug "Python version" "$("$_SMTP_BIN" --version 2>&1)"
  142. # language=Python
  143. "$_SMTP_BIN" <<EOF
  144. # This code is meant to work with either Python 2.7.x or Python 3.4+.
  145. try:
  146. try:
  147. from email.message import EmailMessage
  148. except ImportError:
  149. from email.mime.text import MIMEText as EmailMessage # Python 2
  150. from smtplib import SMTP, SMTP_SSL, SMTPException
  151. from socket import error as SocketError
  152. except ImportError as err:
  153. print("A required Python standard package is missing. This system may have"
  154. " a reduced version of Python unsuitable for sending mail: %s" % err)
  155. exit(1)
  156. show_transcript = """$_SMTP_SHOW_TRANSCRIPT""" == "True"
  157. smtp_host = """$_SMTP_HOST"""
  158. smtp_port = int("""$_SMTP_PORT""")
  159. smtp_secure = """$_SMTP_SECURE"""
  160. username = """$_SMTP_USERNAME"""
  161. password = """$_SMTP_PASSWORD"""
  162. timeout=int("""$_SMTP_TIMEOUT""") # seconds
  163. from_email="""$_SMTP_FROM"""
  164. to_emails="""$_SMTP_TO""" # can be comma-separated
  165. subject="""$_SMTP_SUBJECT"""
  166. content="""$_SMTP_CONTENT"""
  167. try:
  168. msg = EmailMessage()
  169. msg.set_content(content)
  170. except (AttributeError, TypeError):
  171. # Python 2 MIMEText
  172. msg = EmailMessage(content)
  173. msg["Subject"] = subject
  174. msg["From"] = from_email
  175. msg["To"] = to_emails
  176. smtp = None
  177. try:
  178. if smtp_secure == "ssl":
  179. smtp = SMTP_SSL(smtp_host, smtp_port, timeout=timeout)
  180. else:
  181. smtp = SMTP(smtp_host, smtp_port, timeout=timeout)
  182. smtp.set_debuglevel(show_transcript)
  183. if smtp_secure == "tls":
  184. smtp.starttls()
  185. if username or password:
  186. smtp.login(username, password)
  187. smtp.sendmail(msg["From"], msg["To"].split(","), msg.as_string())
  188. except SMTPException as err:
  189. # Output just the error (skip the Python stack trace) for SMTP errors
  190. print("Error sending: %r" % err)
  191. exit(1)
  192. except SocketError as err:
  193. print("Error connecting to %s:%d: %r" % (smtp_host, smtp_port, err))
  194. exit(1)
  195. finally:
  196. if smtp is not None:
  197. smtp.quit()
  198. EOF
  199. }