Mailing script
A mailing script! Here's a basic example of a Python script that sends an email using the smtplib
library:
import smtplib
from email.mime.text import MIMEText
# Define the email settings
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
FROM_EMAIL = "[email protected]"
PASSWORD = "your_password"
# Define the recipient's email address
TO_EMAIL = "[email protected]"
# Define the email subject and body
SUBJECT = "Test Email"
BODY = "This is a test email sent using a Python script."
# Create a text message
msg = MIMEText(BODY)
msg['Subject'] = SUBJECT
msg['From'] = FROM_EMAIL
msg['To'] = TO_EMAIL
# Connect to the SMTP server
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(FROM_EMAIL, PASSWORD)
# Send the email
server.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())
# Close the SMTP connection
server.quit()
This script sends a simple text email to the recipient's email address. You'll need to replace the placeholders ([email protected]
, your_password
, and [email protected]
) with your actual email credentials and the recipient's email address.
Note: This script uses the smtplib
library, which is a built-in Python library. However, it's important to note that this library is not secure and should not be used for production purposes. For a more secure solution, consider using a library like yagmail
or email
with a secure SMTP server.
Here's an example of how you can modify the script to use yagmail
:
import yagmail
# Define the email settings
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
FROM_EMAIL = "[email protected]"
PASSWORD = "your_password"
# Define the recipient's email address
TO_EMAIL = "[email protected]"
# Define the email subject and body
SUBJECT = "Test Email"
BODY = "This is a test email sent using a Python script."
# Create a yagmail object
yag = yagmail.SMTP(FROM_EMAIL, PASSWORD)
# Send the email
yag.send(TO_EMAIL, SUBJECT, BODY)
# Close the yagmail object
yag.close()
This script uses the yagmail
library, which provides a more secure and user-friendly way to send emails. You'll need to install yagmail
using pip: pip install yagmail
.