Python Forum
Help send email by Python using smtplib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help send email by Python using smtplib
#1
I tried using the example code as below:
Quote:smtp_senddata.py
import smtplib
import email.utils
from email.mime.text import MIMEText

# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', '[email protected]'))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = 'Simple test message'

server = smtplib.SMTP('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
try:
    server.sendmail('[email protected]',['[email protected]'],msg.as_string())
finally:
    server.quit()
Quote:smtpd_custom/.py
import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
    
    def process_message(self, peer, mailfrom, rcpttos, data):
        print 'Receiving message from:', peer
        print 'Message addressed from:', mailfrom
        print 'Message addressed to  :', rcpttos
        print 'Message length        :', len(data)
        return

server = CustomSMTPServer(('127.0.0.1', 1025), None)

asyncore.loop()
I run stmpd_custom.py then smtpd_senddata.py.
it run ok but when I check the mail ([email protected]), I don't receive any email. :(
Could you please help me this problem?
Thanks all very much.
Reply
#2
Do you want to use Gmail to send the emails or are you running a local SMTP server?
For Gmail you have to connect to SMTP's Gmail like this:

import smtplib

gmail_user = '[email protected]'
gmail_password = 'mygmailpassword'

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(gmail_user, gmail_password)
except Exception as e:
    print(f'Something went wrong: {e}')
Reply
#3
Can send over SSL @gontajones suggest.
Can also send over TLS on port 587.
Remember that 127.0.0.1(localhost) is a loopback back to you Confused
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

from_addr = "[email protected]"
to_addr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = "Test send mail"

body = "From mars"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'xxxxxxxx')
text = msg.as_string()
server.sendmail(from_addr, to_addr, text)
server.quit()
Output:
Common smtp servers Name Server Authentication Port Gmail smtp.gmail.com SSL 465 Gmail smtp.gmail.com StartTLS 587 Hotmail smtp.live.com SSL 465 Mail.com smtp.mail.com SSL 465 Outlook.com smtp.live.com StartTLS 587 Office365.com smtp.office365.com StartTLS 587 Yahoo Mail smtp.mail.yahoo.com SSL 465
Reply
#4
(Jul-18-2018, 06:40 PM)gontajones Wrote: Do you want to use Gmail to send the emails or are you running a local SMTP server?
For Gmail you have to connect to SMTP's Gmail like this:

import smtplib

gmail_user = '[email protected]'
gmail_password = 'mygmailpassword'

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(gmail_user, gmail_password)
except Exception as e:
    print(f'Something went wrong: {e}')

I want to running a local SMTP server ( I understand it is smtpd_custom.py).

Thanks

(Jul-18-2018, 06:40 PM)gontajones Wrote: Do you want to use Gmail to send the emails or are you running a local SMTP server?
For Gmail you have to connect to SMTP's Gmail like this:

import smtplib

gmail_user = '[email protected]'
gmail_password = 'mygmailpassword'

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(gmail_user, gmail_password)
except Exception as e:
    print(f'Something went wrong: {e}')
Thanks for your supportation
Reply
#5
Quote:I want to running a local SMTP server ( I understand it is smtpd_custom.py).

To send a message to yourself using locathost try the following. Sendmail is one class that I have never felt I should learn, so you are on your own if this doesn't work for you.
   fromaddr = "<root@localhost>"
   toaddrs  = ["your_login_name@localhost"]
   msg = ("This is a test message\n")

   server = smtplib.SMTP('localhost')
   server.set_debuglevel(1)
   server.sendmail(fromaddr, toaddrs, msg)
   server.quit() 
Reply
#6
I can't say for sure but I think the local use of smtpd is just for debug purpose like sending/receiving emails from stdin/stdout.

Check these "solutions", maybe one of them can do the job.
sending-mail-from-localhost-doesnt-work-as-expected
Reply
#7
I have it working with gmail but want to get it working with mail.com or outlook.com. Can anyone assist?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Automated Email aidanh26 10 4,502 Jul-13-2020, 04:09 PM
Last Post: aidanh26
  Send Email with attachment metro17 4 3,247 Apr-14-2020, 04:59 PM
Last Post: CodeItBro
  Send an email Jokoba 0 2,536 Mar-12-2020, 05:30 PM
Last Post: Jokoba
  Send Pressure sensor data to IoT platform using WebSockets in Python barry76 3 4,616 Mar-12-2019, 09:48 AM
Last Post: barry76
  Start tls - Unable to send email darunkumar 7 11,545 May-30-2018, 10:43 AM
Last Post: darunkumar
  sending email with python shahpy 4 14,119 Oct-03-2016, 10:00 PM
Last Post: shahpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020