猿问

使用 python smtp,只有一个收件人收到邮件

我是 python 的新手,下面是假设将邮件发送到多个收件人的代码,但只有 dipeshyog94@gmail.com 正在收到邮件。位于 To 上的 milanthapa898@gmail.com 和位于 cc 上的 alexlee94@gmail.com 没有收到邮件


from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email import encoders

import smtplib


owner_emp_id_email = "dipeshyogi94@gmail.com,milanthapa989@gmail.com"

mymail='milanthapa898@gmail.com'

msg = MIMEMultipart()

msg['From'] = mymail

msg['To'] = owner_emp_id_email

cc_mail = "alexlee94@gmail.com"

msg['Cc'] = cc_mail

print('####44444444444444########\n')

print(owner_emp_id_email)

msg['Subject'] = 'Automated Test Mail with python'

a = 'Milan Thapa'

#body = 'Dear '+spoc_name+',\n\nYou have created new job with below Details:\n\nProject ID : '+project_ID+'\n\nProject Name : '+ibu_name+'\n\nJob Description : ' +job_description +'\n\nThanks and Regards,\n\nMilan Thapa'

html = """\

            <html>

                <head></head>

                    <body>

                        <p>'Dear <b>{}<b>


                        </p>

                    </body>

            </html>

            """.format(a)

msg.attach(MIMEText(html,'html'))

text = msg.as_string()

try:

    server = smtplib.SMTP('smtp.gmail.com:587')


except:

    server = smtplib.SMTP('smtp.gmail.com:587')

server.ehlo()

server.starttls()

server.ehlo()

server.login(mymail,'password')

server.sendmail(mymail,owner_emp_id_email,text)

server.quit()

我陷入了无法将邮件发送给多个用户的问题。


任何帮助将不胜感激!


提前致谢


慕后森
浏览 589回答 3
3回答

小唯快跑啊

标题中的信息不控制消息的实际去向。to 的第二个参数sendmail是唯一可以控制 this 的地方。这个值应该是一个列表,而不是一个逗号分隔的字符串。owner_emp_id_email = "dipeshyogi94@gmail.com,milanthapa989@gmail.com"env_rcpts = owner_emp_id_email.split(",")# ...cc_mail = "alexlee94@gmail.com"env_rcpts.append(cc_mail)# ...server.sendmail(mymail,env_rcpts,text)您会注意到,您还可以添加既不在To:or 中的地址Cc:(或许多其他具有相同目的的标头)以有效实现Bcc:也许还可以看看send_message哪个可以使您不必将消息单独转换为可以传递给的字符串sendmail。

POPMUISE

msg['To']&nbsp;=&nbsp;owner_emp_id_email这owner_emp_id_email是一个string.&nbsp;使其成为电子邮件 ID 列表。然后它会起作用。to_ids&nbsp;=&nbsp;owner_emp_id_email.split(',') msg['To']&nbsp;=&nbsp;to_ids

人到中年有点甜

>>> help(smtplib)to_addrs: A list of addresses to send this mail to. A bare string will be treated as a list with 1 address.您需要将 CSV 字符串转换为另一个答案中提到的列表。有了这个答案,我只想演示我们如何在卡住时使用 python 惊人的帮助功能。
随时随地看视频慕课网APP

相关分类

Python
我要回答