import smtplib
import email
* How email messages should be formatted
* Encrypted
* Relayed between mail servers
* The other details that your computer handles after you click
Provider | SMTP server domain name |
---|---|
Gmail | smtp.gmail.com |
Outlook.com/Hotmail.com | smtp-mail.outlook.com |
Yahoo Mail | smtp.mail.yahoo.com |
import smtplib
smtp = smtplib.SMTP('smtp.gmail.com', 587)
import smtplib
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
(250, 'smtp.gmail.com at your service, [114.32.166.114]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
import smtplib
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
(220, '2.0.0 Ready to start TLS')
import smtplib
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login('My_EmailAddress@gmail.com','My_PassWord')
smtp.sendmail('mycoffeeyoa@gmail.com',
'mycoffeeyoa@gmail.com',
'Subject: Hello World Email!\n\nDear Jamie,\n\nHow are you?\n\nSincerely,\nJamie')
{}
smtp.sendmail('mycoffeeyoa@gmail.com',
['mycoffeeyoa@gmail.com', 'jmw@leadinfo.com.tw'],
'Subject: Hello World Email!\nDear Jamie,\n\nHow are you?\n\nSincerely,\nJamie')
{}
smtp.quit()
(221, '2.0.0 closing connection j5-v6sm7956358pgp.6 - gsmtp')
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com"
text_plain = MIMEText(text,'plain', 'utf-8')
text_plain.add_header("To","mycoffeeyoa@gmail.com")
text_plain.add_header("Subject","Hello World Email!")
smtp.sendmail('mycoffeeyoa@gmail.com',
'mycoffeeyoa@gmail.com',
text_plain.as_string())
{}
from email.mime.text import MIMEText
html = """
<html>
<body>
<p>
Here is the <a href="http://www.baidu.com">link</a> you wanted.
</p>
</body>
</html>
"""
text_html = MIMEText(html, 'html', 'utf-8')
text_html.add_header("To","mycoffeeyoa@gmail.com")
text_html.add_header("Subject","Hello World Email!")
smtp.sendmail('mycoffeeyoa@gmail.com',
'mycoffeeyoa@gmail.com',
text_html.as_string())
{}
from email.mime.image import MIMEImage
sendimagefile=open(r'D4_08.JPG','rb').read()
image = MIMEImage(sendimagefile, 'jpeg')
image["To"] = 'mycoffeeyoa@gmail.com'
image["Subject"] = 'PyLadiesTaiwan_Logo'
smtp.sendmail('mycoffeeyoa@gmail.com','mycoffeeyoa@gmail.com', image.as_string())
{}
- alternative:email includes text/plain or text/html
- related:email includes images or recordings
- mixed:email includes text/plain, text/html images, recordings... and so on
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart('mixed')
msg['Subject'] = 'Greeting from Pyladies!!'
msg['From'] = 'mycoffeeyoa@gmail.com <mycoffeeyoa@gmail.com>'
msg['To'] = 'mycoffeeyoa@gmail.com'
#郵件內文
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com"
msg.attach(MIMEText(text,'plain', 'utf-8'))
#郵件 html 內文
html = """
<html>
<body>
<p>
Here is the <a href="http://www.baidu.com">link</a> you wanted.
</p>
</body>
</html>
"""
msg.attach(MIMEText(html, 'html', 'utf-8'))
#郵件附件圖片
sendimagefile=open(r'D4_08.JPG','rb').read()
image = MIMEImage(sendimagefile, 'jpeg')
msg.attach(image)
smtp.sendmail('mycoffeeyoa@gmail.com',
'mycoffeeyoa@gmail.com',
msg.as_string())
{}
import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText