Emailer sample
Here is a sample emailer in Python 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 email message
SUBJECT = "Test Email"
BODY = "This is a test email sent using Python."
# Create a text message
msg = MIMEText(BODY)
msg["Subject"] = SUBJECT
msg["From"] = FROM_EMAIL
msg["To"] = "[email protected]"
# 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, "[email protected]", msg.as_string())
# Close the SMTP connection
server.quit()
This code sends a simple text email to a recipient using the Gmail SMTP server. 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.
Here's a breakdown of the code:
- We import the
smtplib
library and theMIMEText
class from theemail.mime.text
module. - We define the email settings: the SMTP server, port, from email address, and password.
- We define the email message: the subject, body, and recipient email address.
- We create a
MIMEText
object and set its subject, from, and to fields. - We connect to the SMTP server using the
SMTP
class fromsmtplib
. - We start a TLS connection using the
starttls()
method. - We log in to the SMTP server using the
login()
method. - We send the email using the
sendmail()
method. - We close the SMTP connection using the
quit()
method.
Note that this is just a basic example, and you may want to add additional features such as error handling, attachments, and HTML formatting to your emailer.