9

I'm php programmer and with php you can send email with server directly, for example this code send email to client:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

but in python you have to use smtplib and gmail or hotmail servers for sending email. I wonder is there anyway to send email with python direct from server?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Mohsen Koleyni
  • 109
  • 1
  • 1
  • 8

1 Answers1

11

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 it's listening then you should be able to do something like this from Python to send an email.

import smtplib

sender = 'no_reply@mydomain.com'
receivers = ['person@otherdomain.com']

message = """From: No Reply <no_reply@mydomain.com>
To: Person <person@otherdomain.com>
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")
Neuron
  • 5,141
  • 5
  • 38
  • 59
Robert Putt
  • 476
  • 3
  • 9
  • 1
    can you give me an example how use local SendMail with python? or edit and put some python code to guide me how send email from local machine. – Mohsen Koleyni Jan 14 '19 at 16:08
  • 1
    Sure, first on the host you are using please can you provide the output of ```netstat -tuna``` from your terminal so we can check SendMail is listening locally as expected. – Robert Putt Jan 14 '19 at 16:09
  • 4
    I get this error : `sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it` – Mohsen Koleyni Jan 14 '19 at 16:18
  • 1
    Interesting, as you are using Windows by the seems of it your PHP's php.ini must already have an SMTP server specified, I would recommend extracting this from your PHP environment and re-using the same server from your Python app. – Robert Putt Jan 14 '19 at 16:22
  • @RobertPutt i'm having an issue that is like his check [here](https://stackoverflow.com/questions/60058165/django-send-email-from-a-form-with-the-fillers-email-as-from/60058846#60058846) when i tried your code example in my ubuntu server, i had this error `socket.error: [Errno 111] Connection refused` how can I solve that? btw when i do `netstat -tuna` i don't find anything listening at port 25 – Simou Feb 04 '20 at 14:44
  • I'm getting "Connection refused" error on `smtplib.SMTP('localhost')` – Shrike Feb 28 '23 at 13:19