cardinal_pythonlib.email.sendmail
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Sends e-mails from the command line.
- cardinal_pythonlib.email.sendmail.get_email_domain(email_: str) str [source]
Returns the domain part of an e-mail address.
- cardinal_pythonlib.email.sendmail.is_email_valid(email_: str) bool [source]
Performs some basic checks that a string appears to be an e-mail address.
See https://stackoverflow.com/questions/8022530/how-to-check-for-valid-email-address.
- cardinal_pythonlib.email.sendmail.main() NoReturn [source]
Command-line processor. See
--help
for details.
- cardinal_pythonlib.email.sendmail.make_email(from_addr: str, date: str | None = None, sender: str = '', reply_to: str | List[str] = '', to: str | List[str] = '', cc: str | List[str] = '', bcc: str | List[str] = '', subject: str = '', body: str = '', content_type: str = 'text/plain', charset: str = 'utf8', attachment_filenames: Sequence[str] | None = None, attachment_binaries: Sequence[bytes] | None = None, attachment_binary_filenames: Sequence[str] | None = None, verbose: bool = False) MIMEMultipart [source]
Makes an e-mail message.
Arguments that can be multiple e-mail addresses are (a) a single e-mail address as a string, or (b) a list of strings (each a single e-mail address), or (c) a comma-separated list of multiple e-mail addresses.
- Parameters:
from_addr¶ – name of the sender for the “From:” field
date¶ – e-mail date in RFC 2822 format, or
None
for “now”sender¶ – name of the sender for the “Sender:” field
reply_to¶ – name of the sender for the “Reply-To:” field
to¶ – e-mail address(es) of the recipients for “To:” field
cc¶ – e-mail address(es) of the recipients for “Cc:” field
bcc¶ – e-mail address(es) of the recipients for “Bcc:” field
subject¶ – e-mail subject
body¶ – e-mail body
content_type¶ – MIME type for body content, default
text/plain
charset¶ – character set for body; default
utf8
attachment_filenames¶ – filenames of attachments to add
attachment_binaries¶ – binary objects to add as attachments
attachment_binary_filenames¶ – filenames corresponding to
attachment_binaries
verbose¶ – be verbose?
- Returns:
a
email.mime.multipart.MIMEMultipart
- Raises:
AssertionError –
- cardinal_pythonlib.email.sendmail.send_email(from_addr: str, host: str, user: str, password: str, port: int | None = None, use_tls: bool = True, date: str | None = None, sender: str = '', reply_to: str | List[str] = '', to: str | List[str] = '', cc: str | List[str] = '', bcc: str | List[str] = '', subject: str = '', body: str = '', content_type: str = 'text/plain', charset: str = 'utf8', attachment_filenames: Sequence[str] | None = None, attachment_binaries: Sequence[bytes] | None = None, attachment_binary_filenames: Sequence[str] | None = None, verbose: bool = False) Tuple[bool, str] [source]
Sends an e-mail in text/html format using SMTP via TLS.
- Parameters:
host¶ – mail server host
user¶ – username on mail server
password¶ – password for username on mail server
port¶ – port to use, or
None
for protocol defaultuse_tls¶ – use TLS, rather than plain SMTP?
date¶ – e-mail date in RFC 2822 format, or
None
for “now”from_addr¶ – name of the sender for the “From:” field
sender¶ – name of the sender for the “Sender:” field
reply_to¶ – name of the sender for the “Reply-To:” field
to¶ – e-mail address(es) of the recipients for “To:” field
cc¶ – e-mail address(es) of the recipients for “Cc:” field
bcc¶ – e-mail address(es) of the recipients for “Bcc:” field
subject¶ – e-mail subject
body¶ – e-mail body
content_type¶ – MIME type for body content, default
text/plain
charset¶ – character set for body; default
utf8
attachment_filenames¶ – filenames of attachments to add
attachment_binaries¶ – binary objects to add as attachments
attachment_binary_filenames¶ – filenames corresponding to
attachment_binaries
verbose¶ – be verbose?
- Returns:
(success, error_or_success_message)
- Return type:
tuple
See
Re security:
TLS supersedes SSL: https://en.wikipedia.org/wiki/Transport_Layer_Security
SMTP connections on ports 25 and 587 are commonly secured via TLS using the
STARTTLS
command: https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol“STARTTLS on port 587” is one common method. Django refers to this as “explicit TLS” (its
E_MAIL_USE_TLS
setting; see https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-EMAIL_USE_TLS).Port 465 is also used for “implicit TLS” (3.3 in https://tools.ietf.org/html/rfc8314). Django refers to this as “implicit TLS” too, or SSL; see its
EMAIL_USE_SSL
setting at https://docs.djangoproject.com/en/2.1/ref/settings/#email-use-ssl). We don’t support that here.
- cardinal_pythonlib.email.sendmail.send_msg(from_addr: str, to_addrs: str | List[str], host: str, user: str, password: str, port: int | None = None, use_tls: bool = True, msg: MIMEMultipart | None = None, msg_string: str | None = None) None [source]
Sends a pre-built e-mail message.
- Parameters:
from_addr¶ – e-mail address for ‘From:’ field
to_addrs¶ – address or list of addresses to transmit to
host¶ – mail server host
user¶ – username on mail server
password¶ – password for username on mail server
port¶ – port to use, or
None
for protocol defaultuse_tls¶ – use TLS, rather than plain SMTP?
msg¶ – a
email.mime.multipart.MIMEMultipart
msg_string¶ – alternative: specify the message as a raw string
- Raises:
RuntimeError –
See also: