You can use the following script to send e-mails in 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 25 26 27 28 29 30 31 32 33 34 35 |
import smtplib import email.utils from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() msg['Subject'] = 'Test Subject' msg.preamble = 'Multipart massage.\r\n' # E-mail body part = MIMEText("Test body text") msg.attach(part) # E-mail attachment part = MIMEApplication(open("test.pdf","rb").read()) part.add_header('Content-Disposition', 'attachment', filename="test.pdf") msg.attach(part) # SMTP server server = smtplib.SMTP("smtp.testsmtp.com",25) # SMTP debug server.set_debuglevel(True) # Sending try: server.ehlo() server.login("username", "password") server.sendmail('From', ['To'], msg.as_string()) finally: server.quit() |
Leave a Reply