简体   繁体   English

Python - 使用smtplib发送给多个收件人

[英]Python - using smtplib to send to multiple recipients

I'm finding it very difficult to modify this code to send to send to multiple recipients, I have tried many variations of the examples already out there. 我发现修改此代码以发送给多个收件人非常困难,我已经尝试了许多已经存在的示例。 I'm very new to python so any help would be much appreciated. 我对python很新,所以任何帮助都会非常感激。

It is as part of safety system I am designing to alert parents and carers of potential elopement risk for children and adults with ASD. 这是我设计的安全系统的一部分,旨在提醒父母和照顾者儿童和成人患有ASD的潜在私奔风险。

'''
import time
import serial
import smtplib

TO = 'email@mail.org'
GMAIL_USER = 'email@gmail.com'
GMAIL_PASS = 'passowrd'

SUBJECT = 'Security Alert'
TEXT = 'Movement detected!'

ser = serial.Serial('COM6', 9600)

def send_email():
print("Sending Email")
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(GMAIL_USER, GMAIL_PASS)
header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
header = header + '\n' + 'Subject:' + SUBJECT + '\n'
print header
msg = header + '\n' + TEXT + ' \n\n'
smtpserver.sendmail(GMAIL_USER, TO, msg)
smtpserver.close()

while True:
    message = ser.readline()
    print(message)
    if message[0] == 'M' :
        send_email()
    time.sleep(0.5)

''' “””

Send the alert to multiple people. 将警报发送给多个人。

Have you had a look at this yet? 你有看过这个吗? How to send email to multiple recipients using python smtplib? 如何使用python smtplib向多个收件人发送电子邮件?

It looks like you might be dropping some of the pieces of your header too by overwriting them: 看起来你可能会通过覆盖它们来删除标题的一些部分:

header = 'From: ' + GMAIL_USER

Instead of: 代替:

header = header + 'From: ' + GMAIL_USER

You might also want to consider using format instead, but I'm already out of my depth on Python :-) 您可能还想考虑使用格式,但我已经超出了Python的深度:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM