![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
The smtplibaio package provides an SMTP client session object that can
be used to send e-mail in an asynchronous way (i.e. using asyncio
).
Let's start with a very basic example, using SMTP_SSL
:
import asyncio
from email.headerregistry import Address
from email.message import EmailMessage
from smtplibaio import SMTP, SMTP_SSL
async def send_email():
from_addr = "bob@example.net"
to_addr = "alice@example.org"
message = "Hi Alice !"
async with SMTP_SSL() as client:
await client.sendmail(from_addr, to_addr, message)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(send_email())
loop.close()
As you can see, the Asynchronous Context Manager makes it really easy to use.
STARTTLS is supported only if you have the aioopenssl
module
installed. You must tell SMTP
to use it upon instantiation:
async def send_email():
"""
"""
from_addr = "bob@example.net"
to_addr = "alice@example.org"
message = "Hi Alice !"
async with SMTP(use_aioopenssl=True) as client:
await client.starttls()
await client.sendmail(from_addr, to_addr, message)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(send_email())
loop.close()
In the next example, we are specifying the server hostname and port, we
are using authentication and we are using the objects provided by the
email
package available in the Python Standard Library (i.e.
email.message.EmailMessage
) to build a proper email message.
async def send_email():
# SMTP server:
smtp_server = "smtp.example.org"
port = 587
# Credentials used to authenticate:
username = "alice"
passwd = "5ecreT!"
# Use of Address object is not mandatory:
from_addr = Address("Alice", "alice", "example.org")
to_addr = Address("Bob", "bob", "example.net")
bcc_addr = Address("John", "john", "example.net")
# E-mail subject and content:
subject = "Testing smtplibaio"
content = "Look, all emails sent from this method are BCCed to John !"
# Build the list of recipients (To + Bcc):
recipients = [to_addr.addr_spec, bcc_addr.addr_spec]
# Build the EmailMessage object:
message = EmailMessage()
message.add_header("From", str(from_addr))
message.add_header("To", str(to_addr))
message.add_header("Bcc", str(bcc_addr))
message.add_header("Subject", subject)
message.add_header("Content-type", "text/plain", charset="utf-8")
message.set_content(content)
# Send the e-mail:
async with SMTP_SSL(hostname=smtp_server, port=port) as client:
await client.auth(username, passwd)
await client.sendmail(from_addr.addr_spec, recipients, message.as_string())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(send_email())
loop.close()
You can also have a more fine-grained control using the lower-level methods.
SMTP.ehlo()
;SMTP.helo()
;SMTP.starttls()
(depending on aioopenssl availability)
;SMTP.auth()
(LOGIN, PLAIN and CRAM-MD5 mechanisms are
suported) ;SMTP.mail()
;SMTP.rcpt()
;SMTP.vrfy()
;SMTP.data()
;SMTP.expn()
;SMTP.noop()
;SMTP.quit()
;SMTP.help()
.email.message.EmailMessage
. You can still use
email.message.EmailMessage.as_string()
or
str(email.message.EmailMessage)
instead. See the example above for
further details.We use black, isort in combination with pre-commit to ensure one coding style and reduce the risk of merge conflicts. Please [install pre-commit] to ensure your commits also meet these standards. When you see something to improve, have ideas for better tests or documentation, please create issues or create a pull request.
FAQs
An async version of smtplib
We found that smtplibaio demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.