2017-09-20 36 views
1

我試圖發送電子郵件到專用SMTP服務器,可以做到使用python smtplib如下sendgrid - 如何指定收件人SMTP服務器?

s = smtplib.SMTP(server, port) 
s.sendmail(me, you, msg.as_string()) 

試圖將其轉換到Sendgrid(如天青塊SMTP),但它沒有選項指定一個專用SMTP服務器。

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

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) 
from_email = Email("[email protected]") 
to_email = Email("[email protected]") 
subject = "Sending with SendGrid is Fun" 
content = Content("text/plain", "and easy to do anywhere, even with Python") 
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) 

任何幫助將不勝感激。

感謝

回答

1

這是很正常的,蟒蛇包sendgrid不使用SMTP協議,但實際上它要求使用經典HTTPS請求的API ...

所以,除非您的SMTP服務器可以通過API(定製?)訪問,因此您無法使用sendgrid軟件包。

如果您想自定義的Python包sendgrid爲了向自己的API端點將作爲繼電器您的SMTP請求一點上,你可以自由地修改它的源代碼:https://github.com/sendgrid/sendgrid-python

+0

感謝您的回答。根本問題是SMTP Azure發送在Microsoft Azure上被阻止(我也懷疑AWS),他們建議使用中繼服務,如sendgrid。但是,如果我必須修改sendgrid以使用smtp,我懷疑我會以任何方式遇到Azure阻止問題。 – dopplesoldner

+0

您必須創建一個靠近您的SMTP服務器的API端點來充當中繼。您在HTTPS中從Azure出去並指向API端點。你可以關閉這個問題,我們無法回答你的問題;) –

0

另外,您可以使用端口2525,因爲據我所知,所有主要的smtp供應商都支持它,並且它沒有被Azure阻止。但是如果您的smtp服務器不是來自主要供應商的產品,並且您無權訪問它來更改/添加端口,那麼您別無選擇,只能去Azure以外的服務進行託管,因爲所有主要供應商現在都會阻止25,465,587.