how to send email with python directly from server and without smtp

how to send email with python directly from server and without smtp

Other Options

When you use the mail functionality in PHP it is using the local sendmail of the host you are on, which in turn is simply a SMTP relay locally, of course locally sending emails is not really suggested as these are likely not have a good delivery rate due to DKIM, SPF and other protection mechanisms. If you care about deliverability I would recommend using either

  1. An external SMTP server to send mail via which is correctly configured for the sending domain.

  2. An API such as AWS SES, Mailgun, or equivalent.

If you do not care about deliverability then you can of course use local SendMail from Python, SendMail listens on the loopback address (127.0.0.1) on port 25 just like any other SMTP server, so you may use smtplib to send via SendMail without needing to use an external SMTP server.

Sending Email via Local SMTP

If you have a local SMTP server such as SendMail check it is listening as expected…

netstat -tuna

You should see it listening on the loopback address on port 25.

If its listening then you should be able to do something like this from Python to send an email.

import smtplib

sender = [email protected]
receivers = [[email protected]]

message = From: No Reply <[email protected]>
To: Person <[email protected]>
Subject: Test Email

This is a test e-mail message.


try:
    smtp_obj = smtplib.SMTP(localhost)
    smtp_obj.sendmail(sender, receivers, message)         
    print(Successfully sent email)
except smtplib.SMTPException:
    print(Error: unable to send email)

how to send email with python directly from server and without smtp

Leave a Reply

Your email address will not be published. Required fields are marked *