Daily Email Report
Daily Email Report
libraries. Below is a step-by-step guide and script to help you set it up.
Step-by-Step Guide
python
Copy code
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from datetime import datetime
def send_email_report():
# Email configuration
smtp_server = 'smtp.gmail.com'
smtp_port = 587
sender_email = '[email protected]'
sender_password = 'your_password'
recipient_email = '[email protected]'
# Email content
subject = f"Daily Report - {datetime.now().strftime('%Y-%m-%d')}"
body = "This is the daily report."
try:
# Connect to the SMTP server and send email
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
server.close()
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")
if __name__ == "__main__":
send_email_report()
1. Import Libraries:
o smtplib: For sending email using the SMTP protocol.
o email: For creating the email content.
o datetime: For getting the current date to include in the email subject.
2. Email Configuration:
o smtp_server, smtp_port: SMTP server details.
o sender_email, sender_password: Credentials of the sender email.
o recipient_email: Email address of the recipient.
3. Email Content:
o subject, body: Subject and body of the email.
o MIMEMultipart(): Create a multipart email message.
o MIMEText(): Attach the email body.
o MIMEApplication(): Attach a file to the email.
4. Send the Email:
o Connect to the SMTP server using smtplib.SMTP().
o Start TLS encryption using starttls().
o Log in to the email account using login().
o Send the email using sendmail().
o Close the connection using close().
sh
Copy code
crontab -e
2. Add the following line to run the script daily at a specific time (e.g., 8 AM):
sh
Copy code
0 8 * * * /usr/bin/python3 /path/to/your/script.py
With this setup, your script will run daily at the specified time and send the email report
automatically.