Skip to content

Instantly share code, notes, and snippets.

@okineadev
Created April 8, 2025 18:11
Show Gist options
  • Save okineadev/e3ec4752e7147d51282eb34e6ddb7eae to your computer and use it in GitHub Desktop.
Save okineadev/e3ec4752e7147d51282eb34e6ddb7eae to your computer and use it in GitHub Desktop.
πŸ“§ Python script for sending email
import smtplib
from email.mime.text import MIMEText
from typing import List
def send_email(
smtp_server: str,
smtp_port: int,
email: str,
password: str,
recipients: List[str],
subject: str,
body: str
) -> None:
"""
Sends an email to one or more recipients using SMTP over SSL.
Args:
smtp_server (str): The SMTP server address (e.g., "smtp.gmail.com").
smtp_port (int): The SMTP server port (usually 465 for SSL).
email (str): The sender's email address.
password (str): The sender's email password or app-specific password.
recipients (List[str]): A list of recipient email addresses.
subject (str): The subject of the email.
body (str): The plain text content of the email.
Returns:
None
"""
for recipient in recipients:
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = email
msg["To"] = recipient
try:
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
server.login(email, password)
server.sendmail(email, recipient, msg.as_string())
print(f"βœ… Email sent successfully to {recipient}.")
except Exception as e:
print(f"❌ Failed to send email to {recipient}: {e}")
if __name__ == "__main__":
# Example usage β€” replace values with actual credentials and data
send_email(
smtp_server="smtp.example.com",
smtp_port=465,
email="[email protected]",
password="yourpassword",
recipients=["[email protected]", "[email protected]"],
subject="Hello from Python!",
body="Hi there!"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment