相关疑难解决方法(0)

如何使用python smtplib向多个收件人发送电子邮件?

经过多次搜索后,我无法找到如何使用smtplib.sendmail发送给多个收件人.问题是每次发送邮件时邮件标题似乎包含多个地址,但事实上只有第一个收件人才会收到电子邮件.

问题似乎是email.Message模块期望与smtplib.sendmail()函数不同的东西.

简而言之,要发送给多个收件人,您应将标头设置为逗号分隔的电子邮件地址字符串.但该sendmail()参数to_addrs应该是电子邮件地址列表.

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib

msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()
Run Code Online (Sandbox Code Playgroud)

python email message smtp smtplib

180
推荐指数
12
解决办法
20万
查看次数

如何用Python发送电子邮件?

此代码可以正常工作并向我发送电子邮件:

import smtplib
#SERVER = "localhost"

FROM = 'monty@python.com'

TO = ["jon@mycompany.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将其包装在这样的函数中:

def sendMail(FROM,TO,SUBJECT,TEXT,SERVER):
    import smtplib
    """this is some test documentation in the function"""
    message = """\
        From: %s
        To: …
Run Code Online (Sandbox Code Playgroud)

python email function smtplib

155
推荐指数
8
解决办法
26万
查看次数

使用SMTP从Python发送邮件

我正在使用以下方法使用SMTP从Python发送邮件.这是正确的使用方法还是我遗失了?

from smtplib import SMTP
import datetime

debuglevel = 0

smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('YOUR.MAIL.SERVER', 26)
smtp.login('USERNAME@DOMAIN', 'PASSWORD')

from_addr = "John Doe <john@doe.net>"
to_addr = "foo@bar.com"

subj = "hello"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )

message_text = "Hello\nThis is a mail from your server\n\nBye\n"

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" 
        % ( from_addr, to_addr, subj, date, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
Run Code Online (Sandbox Code Playgroud)

python smtp

107
推荐指数
7
解决办法
19万
查看次数

使用gmail和python发送邮件时的SMTPAuthenticationError

当我尝试使用gmail发送邮件并发生python错误时,此类问题已经存在于此站点中,但对我没有帮助

gmail_user = "me@gmail.com"
gmail_pwd = "password"
TO = 'friend@gmail.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
        'From: %s' % gmail_user,
        'Subject: %s' % SUBJECT,
        '', TEXT])

server.sendmail(gmail_user, [TO], BODY)
print ('email sent')
Run Code Online (Sandbox Code Playgroud)

错误:

    server.login(gmail_user, gmail_pwd)
    File "/usr/lib/python3.4/smtplib.py", line 639, in login
   raise SMTPAuthenticationError(code, resp)
   smtplib.SMTPAuthenticationError: (534, b'5.7.14   
   <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14       Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web …
Run Code Online (Sandbox Code Playgroud)

python smtplib

60
推荐指数
3
解决办法
9万
查看次数

登录凭据无法使用Gmail SMTP

我正在尝试通过Gmail发送Python电子邮件.这是我的代码:

import smtplib


fromaddr = '......................'  
toaddrs  = '......................'  
msg = 'Spam email Test'  

username = '.......'  
password = '.......'

server = smtplib.SMTP('smtp.gmail.com', 587)  
server.ehlo()
server.starttls()
server.login(username, password)  
server.sendmail(fromaddr, toaddrs, msg)  
server.quit()
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Traceback (most recent call last):
  File "email_send.py", line 18, in <module>
    server.login(username, password)
  File "C:\.....\Python\lib\smtplib.py", line 633
, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepte
d. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=1425
7\n5.7.8 {BADCREDENTIALS} s10sm9426107qam.7 - gsmtp')
Run Code Online (Sandbox Code Playgroud)

这似乎是登录的问题.我确信我的登录详细信息是正确的,除了一件事.用户名应该是"blah@gmail.com",还是只是"等等"?我试过两个,同样的错误.

有什么想法错了吗?

注意:所有句点都代替密码/电子邮件/文件路径/等.

python smtp login smtp-auth

27
推荐指数
8
解决办法
6万
查看次数

Gmail SMTP 服务器停止工作,因为它不再支持“不太安全的应用程序”

我正在开发 Laravel 项目,突然电子邮件停止工作。搜索后,我发现 Google 停止了对不太安全的应用程序的支持,现在我不再在“我的帐户”中看到启用或禁用它的选项。

但现在我不知道如何让它再次工作,因为我在互联网上搜索的所有结果都与旧方法相关。我想让它在本地主机上运行。

这是我找到的更详细的链接 - https://support.google.com/accounts/answer/6010255

php email gmail smtp laravel

15
推荐指数
1
解决办法
2万
查看次数

使用Python和Gmail作为提供商发送电子邮件的安全方式是什么?

我正在尝试使用Python脚本向自己发送电子邮件,幸运的是我遇到了这篇文章:

如何使用Python以Gmail作为提供商发送电子邮件?

麻烦的是,smtplib以纯文本形式发送脚本的密码,我对它的安全性持怀疑态度.此外,我的脚本以纯文本格式包含我的用户名和密码.有没有什么好方法可以使用Python并发送电子邮件而无需将我的密码保留为纯文本?

我也在StackOverflow上看到了这一点: Python smtplib安全性, 但答案并不能完全帮助我解决这个冲突.但是,我还没准备好放弃.


更多信息:我正在尝试将我的Raspberry Pi设置为通过网站进行擦除的服务器.当关于网站的具体事情发生变化时,我希望通过电子邮件收到通知.但是,我不想让我的Pi坐在一个带有我的用户名和密码的纯文本脚本.

python email gmail smtplib raspberry-pi

14
推荐指数
1
解决办法
8345
查看次数

无法发送电子邮件 - Django

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = 'rakil@gmail.com' 
EMAIL_HOST_PASSWORD = '*******' 
DEFAULT_FROM_EMAIL = 'testing@testing.com
Run Code Online (Sandbox Code Playgroud)

但邮件没有发送到地址,在print.html我点击send_email时打印机正在打印,但是它没有发送任何电子邮件.我正在使用Django 1.3.7和Python 2.6.

我不知道问题是版本还是一些逻辑问题.需要帮忙.

django django-templates django-models django-views

11
推荐指数
1
解决办法
8343
查看次数

Django通过smtp.gmail.com发送电子邮件的麻烦

我正在尝试配置Django的send_email,以便我可以向用户发送密码重置电子邮件.到目前为止,我没有运气让它上班.我已经设置了一个基本的Gmail帐户(没有Google App等),并且在我的Django settings.py中我有:

EMAIL_HOST      = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_HOST_USER = 'my_account@gmail.com'
EMAIL_PORT      = 587
MAIL_USE_TLS   = True

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL  = 'admin@my-site.com'
Run Code Online (Sandbox Code Playgroud)

然后我尝试通过这样做来测试:

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test', 'test', 'test@test.com', ['my-personal-email@gmail.com'])
Run Code Online (Sandbox Code Playgroud)

我收到错误信息

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\__init__.py
", line 62, in send_mail
    connection=connection).send()
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\message.py"
, line 255, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Program Files\Python27\lib\site-packages\django\core\mail\backends\sm
tp.py", line 88, in send_messages
    new_conn_created = …
Run Code Online (Sandbox Code Playgroud)

python django smtp

9
推荐指数
4
解决办法
1万
查看次数

无法使用gmail通过python发送电子邮件 - smtplib.SMTPException:服务器不支持SMTP AUTH扩展

我只想在python中发送带附件的电子邮件

import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
    assert type(send_to)==list
    assert type(files)==list

    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for f in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(f,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        msg.attach(part)

    smtp = smtplib.SMTP('smtp.gmail.com:587')
    smtp.login('fu@gmail.com','fu')
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close() …
Run Code Online (Sandbox Code Playgroud)

python email smtp smtplib python-2.7

9
推荐指数
1
解决办法
3万
查看次数

通过 Python 电子邮件库发送电子邮件会引发错误“预期字符串或类似字节的对象”

我正在尝试通过 python 3.6 中的一个简单函数将 csv 文件作为附件发送。

from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def email():


    msg = MIMEMultipart()
    msg['Subject'] = 'test'
    msg['From'] = 'test@gmail.com'
    msg['To'] = 'testee@gmail.com'
    msg.preamble = 'preamble'

    with open("test.csv") as fp:
        record = MIMEText(fp.read())
        msg.attach(record)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login("test@gmail.com", "password")
    server.sendmail("test@gmail.com", "testee@gmail.com", msg)
    server.quit()
Run Code Online (Sandbox Code Playgroud)

调用email()产生错误expected string or bytes-like object。重新定义server.sendmail("test@gmail.com", "testee@gmail.com", msg)server.sendmail("atest@gmail.com", "testee@gmail.com", msg.as_string()) 会导致发送电子邮件,但会在电子邮件正文中发送 csv 文件,而不是作为附件发送。谁能给我一些关于如何将 csv 文件作为附件发送的提示?

python csv email smtplib python-3.x

8
推荐指数
1
解决办法
4472
查看次数

Python-发送电子邮件时出错(拒绝中继访问)

尝试使用python sendmail发送电子邮件时出现错误。这是我的python代码:(Pas d'objet)

#! /usr/bin/python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "email@gmail.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html …
Run Code Online (Sandbox Code Playgroud)

python email smtp

8
推荐指数
1
解决办法
2086
查看次数

Django 电子邮件:客户端无权作为此发件人发送

我可以使用此 SO 答案中提供的功能通过 smtplib 发送电子邮件:https : //stackoverflow.com/a/12424439/614770

from __future__ import print_function

def send_email(user, pwd, recipient, subject, body):
    import smtplib

    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ', '.join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP('smtp.office365.com', 587)
        server.ehlo()
        server.starttls()
        server.login(user, pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print('Successfully sent the mail')
    except:
        print('Failed to send mail')


if __name__ == …
Run Code Online (Sandbox Code Playgroud)

python django smtplib office365

2
推荐指数
1
解决办法
2981
查看次数