So I have a server that connects to a client. The client requests an email to be sent by the server and the server sends the email. The email is successfully sent but when the server responds to the email sent, it times out.
emailsender = smtplib.SMTP('smtp.gmail.com',587)
emailsender.ehlo()
emailsender.starttls()
try:
emailsender.sendmail(gmailaccount,email,emailmsg)
emailsender.quit
except:
serverSocket.sendto('Unable to find slots',address)
continue
serverSocket.sendto('Successfully booked meeting',address)
The email sender works because I was able to recieve the email. If I don't send the email the client recieves the serverSocket but if the email is sent, the client doesn't recieve the serverSocket. I think I figured out the problem because when the email is sent, the client recieves that. How can i prevent that from happenning? Here is the Client side
try:
clientSocket.sendto(msg(host,port))
response,address = clientSocket.recvfrom(1024)
except:
print 'Timed out'
Related
I have been trying to send emails using SMTP but whenever I send an email it sends the email in bcc instead of sending the email in TO.
the email
import smtplib
my_email= 'exaMPle#gmail.com'
password= '333'
connection = smtplib.SMTP_SSL("smtp.gmail.com",465)
recive='recieve#gmail.com'
connection.ehlo()
connection.login(user=my_email,password=password)
connection.sendmail(my_email,to_addrs=recive,msg='Subject:msg \nhello')
connection.close()
I have a python code which crawls some websites and send me some emails.
The code is working fine on my computer. Now I am trying to run the same code on an Amazon web server. However, google blocks the code. Is there any workaround?
import smtplib
# Send email to my personal email address
def send_email(subject, msg):
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login('e******#gmail.com', 'S****')
message = 'Subject: {}\n\n{}'.format(subject, msg)
server.sendmail('e*#gmail.com', 'e*#gmail.com', message)
server.quit()
print("Success: Email sent!")
except:
print("Email failed to send.")
As answered in the comment section by Furas, Gmail may blocks access from untrusted programs and the programmer may have to create a separated password for these programs. Allow less secure apps to access your Gmail account
I've written a small smtp server in python (taken from the smtpd docs):
import smtpd
import asyncore
class MySMTPServer(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 = MySMTPServer(('127.0.0.1', 587), None)
asyncore.loop()
I want to be able to send an email from my gmail account (or whatever email provider) to this server. Here's what I've done so far:
Write an MX record on an A-Level domain
Throw the server up on a box that has port 587 opened
Send an email from gmail to someone#myaleveldomain.com
Profit?
Step 4 hasn't really come through. I can't find any logs that offer any info into what happened, according to my server logs the message from gmail never arrived.
Am I doing something wrong? Please help!
I've got a simple SMTP mailing example from here and my modified it. My code is:
import smtplib
sender = 'igor.savinkin#gmail.com'
receivers = ['igor.savinkin#gmail.com']
message = """From: From Igor <igor.savinkin#gmail.com>
To: To Igor Savinkin <igor.savinkin#gmail.com>
Subject: SMTP e-mail test
This is a test e-mail message from SMTP python.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email to " + receivers.__str__()
except SMTPException:
print "Error: unable to send email"
Output is:
Successfully sent email to ['igor.savinkin#gmail.com']
Yet, in actuality I find no mails like these in my inbox. The spam folder is checked too!
What's wrong? I use rhc (OpenShift) platform.
You send your mail to a local SMTP server (smtpObj = smtplib.SMTP('localhost')). IMHO it accepts it (request is syntactically correct) but is then not allowed (or not configured) to forward it to gmail. I'm not using OpenShift so I do not know how SMTP is configured there.
You should control how the local SMTP server is configured.
I have a python script to run a SMTP server in localhost. This is my very simple code:
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)
server = CustomSMTPServer(('127.0.0.1', 25), None)
asyncore.loop()
If I send an email from an email client running on localhost the email arrives successfully in the STMP server. However, if I send an email from an email client running in a computer located in the same local network (192.168.1.1/24), it doesn't succeed. Here below the error I get from Outlook Express:
The connection to the server has failed. Account 'localhost', Server '192.168.1.115'.
Protocol SMTP, Port: 25, Secure(SSL): No, Socket Error: 10061, Error Number: 0x800CCC0E
Just in case, I deactivated McAfee firewall in both PCs but I still get the same error.
Where can be the problem? Does it have anything to do with the asyncore.loop() method? Thanks!
Your server is running on the loopback interface:
server = CustomSMTPServer(('127.0.0.1', 25), None)
That interface is not reachable from any external network, only from the local machine.
You will need to start your email server on a real network interface (such as 192.168.1.115, based on the error message).
Also, I doubt you'll be able to retrieve any message anyway. You are running an SMTP server: it accepts messages over SMTP but will not provided POP3 / IMAP services, so you can't retrieve messages using a remote email client. The SMTP server can be used to store messages in a local file-based message store though (and en email client running on the same machine could retrieve messages from the file, if correctly formatted).