dopplesoldner
dopplesoldner

Reputation: 9479

sendgrid - how to specify recipient SMTP server?

I am trying to send emails to a private SMTP server and can do so using python smtplib as follows

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

Trying to convert the same to Sendgrid (as Azure blocks SMTP) but it has no option to specify a private SMTP server.

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)

Any help would be much appreciated.

Thanks

Upvotes: 1

Views: 1003

Answers (2)

Kalana Demel
Kalana Demel

Reputation: 3266

Alternatively you can use port 2525 since all major smtp vendors support it and its not blocked by Azure as far as I know. But if your smtp server is something not from a major vendor and you do not have access to it to change/add ports you have no option but to go to a service other than Azure for hosting which might be hard since all major vendors now block 25, 465, 587.

Upvotes: 0

Jonathan DEKHTIAR
Jonathan DEKHTIAR

Reputation: 3536

That's quite normal, the python package for sendgrid does not use the smtp protocol, but actually request an API which use classical HTTPS requests...

So, except if your smtp server can be accessed over an API (custom-made?), there's no chance you can use the sendgrid package.

If you want to customise the python package for sendgrid in order to point toward your own API endpoint which will act as relay for your smtp requests, you are free to modify its source code: https://github.com/sendgrid/sendgrid-python

Upvotes: 2

Related Questions