Sendgrid集成 - 没有邮件发送

时间:2017-06-09 23:21:00

标签: python email sendgrid

我最近创建了一个SendGrid帐户,我想集成SendGrid for Python。

我尝试过两种类型的集成:Web API和SMTP Relay - 它们似乎都不起作用。我的消息未送达,因此我无法完成整合。 SendGrid说我的电子邮件还没有出现'。

enter image description here

1。这是我用来测试WebAPI集成的脚本(是的,设置了环境变量):

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("me@gmail.com")
to_email = Email("me@gmail.com")
subject = "Sendgrid test"
content = Content("text/plain", "Just a test")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

输出:

202

Server: nginx
Date: Fri, 09 Jun 2017 23:16:41 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 0
Connection: close
X-Message-Id: nyLE-nOUQrSWyw84qQ-xuw
X-Frame-Options: DENY
Access-Control-Allow-Origin: https://sendgrid.api-docs.io
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
Access-Control-Max-Age: 600
X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html

2。这是SMTP中继集成测试的脚本:

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

msg=MIMEMultipart()
msg['From']='Test'
msg['Subject']='This is a test message.'
msg.attach(MIMEText('Hello World'))

fromaddr = 'me@gmail.com'
toaddrs  = 'me@gmail.com'

username = 'apikey'
password = "SG.[cut].[cut]"

server = smtplib.SMTP('smtp.sendgrid.net:587')
server.set_debuglevel(1)
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()

调试输出:

send: 'ehlo [ip.address]\r\n'
reply: '250-smtp.sendgrid.net\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 31457280\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 AUTH=PLAIN LOGIN\r\n'
reply: retcode (250); Msg: smtp.sendgrid.net
8BITMIME
PIPELINING
SIZE 31457280
STARTTLS
AUTH PLAIN LOGIN
AUTH=PLAIN LOGIN
send: 'STARTTLS\r\n'
reply: '220 Begin TLS negotiation now\r\n'
reply: retcode (220); Msg: Begin TLS negotiation now
send: 'ehlo [ip.address]\r\n'
reply: '250-smtp.sendgrid.net\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 31457280\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 AUTH=PLAIN LOGIN\r\n'
reply: retcode (250); Msg: smtp.sendgrid.net
8BITMIME
PIPELINING
SIZE 31457280
STARTTLS
AUTH PLAIN LOGIN
AUTH=PLAIN LOGIN
send: 'AUTH PLAIN AGFwaWtleQBTRy53T2M1YnN2NVJKbXlYczNoMkJYMFV3LmVHc0ZIbkZWUTViM2xfSlM2V0hUd0xwU1JFendsSVZaMnVralI1ZllxTHc=\r\n'
reply: '235 Authentication successful\r\n'
reply: retcode (235); Msg: Authentication successful
send: 'mail FROM:<me@gmail.com> size=293\r\n'
reply: '250 Sender address accepted\r\n'
reply: retcode (250); Msg: Sender address accepted
send: 'rcpt TO:<me@gmail.com>\r\n'
reply: '250 Recipient address accepted\r\n'
reply: retcode (250); Msg: Recipient address accepted
send: 'data\r\n'
reply: '354 Continue\r\n'
reply: retcode (354); Msg: Continue
data: (354, 'Continue')
send: 'Content-Type: multipart/mixed; boundary="===============1872430840=="\r\nMIME-Version: 1.0\r\nFrom: Test\r\nSubject: This is a test message.\r\n\r\n--===============1872430840==\r\nContent-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\nHello World\r\n--===============1872430840==--\r\n.\r\n'
{}
reply: '250 Ok: queued as -u6z[...]_Yg\r\n'
reply: retcode (250); Msg: Ok: queued as -u6z[...]_Yg
data: (250, 'Ok: queued as -u6z[...]_Yg')
send: 'quit\r\n'
reply: '221 See you later\r\n'
reply: retcode (221); Msg: See you later

正如我在两种情况下所述,我的电子邮件因某些原因未送达。我的配置中是否缺少某些内容?

0 个答案:

没有答案