Created
April 8, 2025 18:11
-
-
Save okineadev/e3ec4752e7147d51282eb34e6ddb7eae to your computer and use it in GitHub Desktop.
π§ Python script for sending email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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