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()
Related
I have tried a simple email script in python to understand the functioning. The code is
import smtplib
sender = 'avin#gmail.com'
receivers = ['avin.b#vipointsolutions.net']
message = "This is a test e-mail message."
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except Exception:
print("error")
When i try to run this i got the message Successfully sent email but the email is not delivered to my inbox.I have set up postfix running on my local machine at port 25.Can anyone guide me with the reason on why the email is not receiving.Is it because of the code? Any help would be appreciated.
You have to provide SMTP of the email sender and you have to login from the sender email.
example:
sender = 'avin#gmail.com'
receivers = 'avin.b#vipointsolutions.net'
msg_format = "Hello, Test email"
server = smtplib.SMTP('smtp.office365.com')
server.starttls()
server.verify(receivers)
server.login(sender, 'password')
server.sendmail(sender, receivers, msg_format)
server.quit()
print("Successfully sent email")
My email is: sarahconnor#gmail.com
I would like to send an email from johnconnor#skynet.com to mine.
I don't want to login remotely to my gmail account to send an email.
This is what I need:
I'm Sarah Connor, and I want to receive in my mailbox ( sarahconnor#gmail.com ), an e-mail from johnconnor#skynet.com ...
So I've use this script to do so:
import requests
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
COMMASPACE = ', '
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
fromm = "johnconnor#skynet.com"
to = "sarahconnor#gmail.com"
msg['From'] = fromm
msg['To'] = COMMASPACE.join(to)
msg.preamble = 'Our family reunion'
requests.get("http://smtp.gmail.com", verify=False)
s = smtplib.SMTP_SSL("smtp.gmail.com", 587)
s.starttls()
s.login('sarahconnor#gmail.com', 'mypassword12345') #here I login to the SMTP server from Google to be able to send emails...
s.sendmail(fromm, to, msg.as_string())
s.close()
I have the following error:
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))
And as seen from here, it doesn't seem that I'm having any of that issues.
Does anyone knows how can I fix this?
Thanks
this sends a mail from a gmail account to any other account
import smtplib
from email.mime.text import MIMEText
class GmailHandler():
"""
IMPORTANT NOTE:
in order to access a gmail account with this handler,
your account needs 'foreign-access' enabled (follow these steps):
login to the account
go here--> https://accounts.google.com/b/0/DisplayUnlockCaptcha
press 'Continue'
Done.
"""
def __init__(self, gmail, password):
self.gmail = gmail
self.password = password
def send_mail(self, receivers, subject, text):
if not isinstance(receivers, list):
receivers = [receivers]
# Send the message via our own SMTP server, but don't include the envelope header
smtp = smtplib.SMTP("smtp.gmail.com", 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(self.gmail, self.password)
for receiver in receivers:
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = self.gmail
msg['To'] = receiver
smtp.sendmail(self.gmail, receiver, str(msg))
smtp.quit()
Use smtplib.SMTP instead of smtplib.SMTP_SSL.
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 am new to python, when am trying to sending mail by using python my program is bellow
import smtplib
from smtplib import SMTP
sender = 'raju.ab#gmail.com'
receivers = ['sudeer.p#eunoia.in']
message = """ this message sending from python
for testing purpose
"""
try:
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login(username,password)
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
print "Successfully sent email"
except smtplib.SMTPException:
print "Error: unable to send email"
when i execute it shows Error: unable to send mail message, how to send email in python please explain
What i have done in code :
1.Added a error object to get the error message
import smtplib
from smtplib import SMTP
try:
sender = 'xxx#gmail.com'
receivers = ['xxx.com']
message = """ this message sending from python
for testing purpose
"""
smtpObj = smtplib.SMTP(host='smtp.gmail.com', port=587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login('xxx','xxx')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
If u ran this code u would see a error message like this stating that google is not allowing u to login via code
Things to change in gmail:
1.Login to gmail
2.Go to this link https://www.google.com/settings/security/lesssecureapps
3.Click enable then retry the code
Hopes it help :)
But there are security threats if u enable it
as is said here: How to send an email with Gmail as provider using Python?
This code works. But GMAIL wil warn you if you want to allow this script send the email or not. Sign in in your account, and accesss this URL: https://www.google.com/settings/security/lesssecureapps
import smtplib
gmail_user = "yourmail#gmail.com"
gmail_pwd = "mypassword"
FROM = 'yourmail#gmail.com'
TO = ['receiber#email.com'] #must be a list
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
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'