python 通过Python发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 通过Python发送电子邮件相关的知识,希望对你有一定的参考价值。

import smtplib

with smtplib.SMTP('smtp.gmail.com',587) as smtp:
    
    
    smtp.starttls()
 
    

    smtp.login("dibaloke66@gmail.com","password")

    subject="Test for sending a email via python for measurment project"
    body=" Alert ! Security system activated in your Smart home"


    msg=f'Subject: {subject} \n \n {body}'


    smtp.sendmail("dibaloke66@gmail.com","dibaloke66@gmail.com",msg)    
python -m smtpd -c DebuggingServer -n localhost:1025
import smtplib

with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    subject="Test for sending a email via python for measurment project"
    body=" Alert ! Security system activated in your Smart home"


    msg=f'Subject: {subject}\n \n{body}'


    smtp.sendmail("dibaloke66@gmail.com","dibaloke66@gmail.com",msg)    
import smtplib
from email.message import EmailMessage

msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']='dibaloke66@gmail.com'
msg.set_content('Alert ! Security system activated in your Smart home')
with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    smtp.send_message(msg)
import smtplib
import imghdr
from email.message import EmailMessage

msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']='dibaloke66@gmail.com'
msg.set_content('Alert ! Security system activated in your Smart home')


//make sure the image exists at root of your project
with open('medical-cyber-security-breaches.jpg','rb') as f:
    file_data = f.read()
    file_type = imghdr.what(f.name)
    file_name = f.name 
    
    # print(file_type)
msg.add_attachment(file_data,maintype='image',subtype=file_type,filename=file_name)    
with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    smtp.send_message(msg)
import smtplib
import imghdr
from email.message import EmailMessage

msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']='dibaloke66@gmail.com'
msg.set_content('Alert ! Security system activated in your Smart home')
files=['img_1.jpg','img_2.jpg']

for file in files:
    with open(file,'rb') as f:
        file_data = f.read()
        file_type = imghdr.what(f.name)
        file_name = f.name 
    
    # print(file_type)
    msg.add_attachment(file_data,maintype='image',subtype=file_type,filename=file_name)    

with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    smtp.send_message(msg)
import smtplib
import imghdr
from email.message import EmailMessage

msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']='dibaloke66@gmail.com'
msg.set_content('Alert ! Security system activated in your Smart home')
files=['resume.pdf']

for file in files:
    with open(file,'rb') as f:
        file_data = f.read()
     
        file_name = f.name 
    
    # print(file_type)
    msg.add_attachment(file_data,maintype='application',subtype='octet-stream',filename=file_name)    

with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    smtp.send_message(msg)
import smtplib
import imghdr
from email.message import EmailMessage

contacts=['dibaloke66@gmail.com','a@gmail.com','b@gmail.com']
msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']=contacts
msg.set_content('Alert ! Security system activated in your Smart home')
files=['resume.pdf']

for file in files:
    with open(file,'rb') as f:
        file_data = f.read()
     
        file_name = f.name 
    
    # print(file_type)
    msg.add_attachment(file_data,maintype='application',subtype='octet-stream',filename=file_name)    

with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    smtp.send_message(msg)
import smtplib
import imghdr
from email.message import EmailMessage

msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']='dibaloke66@gmail.com'
msg.set_content('Alert ! Security system activated in your Smart home')
msg.add_alternative("""\
    <!DOCTYPE html>
<html>
    <body>
        <h1 style="color:SlateGray;">This is an HTML Email!</h1>
    </body>
</html>
""",subtype='html')

with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    
    smtp.login("dibaloke66@gmail.com","password")

    smtp.send_message(msg)

以上是关于python 通过Python发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

Python 通过 SMTP 发送邮件

python python脚本通过gmail发送电子邮件

通过 Python 发送 Outlook 电子邮件?

Python 无法通过 iRedMail 发送电子邮件

Python:试图通过电子邮件发送附件〜字符串错误?

通过python脚本向电子邮件发送错误[重复]