A union of curiosity and data science

Knowledgebase and brain dump of a database engineer

send mail python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
smtp_server = "smtp.company.com"
smtp_port = 587  # Use 587 for STARTTLS
 
sender_email = "your-email@company.com"
recipient_email = "recipient@example.com"
subject = "Test Email with TLS"
body = "This email was sent with TLS encryption."
 
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = recipient_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))
 
try:
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        # Enable TLS encryption
        server.starttls(context=ssl.create_default_context())
        # Send email (no login required for relay)
        server.sendmail(sender_email, recipient_email, message.as_string())
        print("Email sent successfully with TLS!")
         
except Exception as e:
    print(f"Error sending email: {e}")