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"
Related
async def test(ctx, arg1, arg2):
_smpt = smtplib.SMTP('mail.cock.li', 587)
_smpt.starttls()
username = ('my email#cock.li')
password = ('my pass')
try:
_smpt.login(username, password)
except:
await ctx.send(f"incorrect password or email")
reciever = (f'{arg1}')
message = (f'{arg2}')
_smpt.sendmail(username, reciever, message)
does anyone know why this is gving me a error im using https://cock.li/server and discord.py bot command
the error is SMTPResponseException: (454, b'4.7.0 TLS not available due to local problem')
Note: change the security settings of your sender mail id such that it allows access to low security apps(can be done manually in mail settings)
Use the following code
import smtplib
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
sender_email_id=input("sender mail")
sender_email_id_password=input("sender mail password")
s.login(sender_email_id, sender_email_id_password)
message = "Message_you_need_to_send"
receiver_email_id=input("receiver mail")
s.sendmail(sender_email_id, receiver_email_id, message)
s.quit()
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")
could someone kindly tell me, what's wrong with the code below? TIA :)))
import smtplib
if raw_input("if you want to send a message from a gmail account, type yes: ") == 'yes':
try:
sender = raw_input("from:\n")
senders_pwd = raw_input("password:\n")
recipient = raw_input("to:\n")
print 'ok, now compile your message:'
subject = raw_input("subject:\n")
body = raw_input("your message:\n")
message = "subject: %s\n%s" %(subject,body)
server = smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
server.ehlo()
print "ok, I've sent your email"
except:
print 'failed to send'
You need to call the sendmail() function. Add something like these three lines after the last server.ehlo():
server.login(sender, senders_pwd)
server.sendmail(sender, recipient, message)
server.close()
I can make smtplib send to other email addresses, but for some reason it is not delivering to my phone.
import smtplib
msg = 'test'
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login("<username>","<password>")
server.sendmail(username, "<number>#vtext.com", msg)
server.quit()
The message sends successfully when the address is a gmail account, and sending a message to the phone using the native gmail interface works perfectly. What is different with SMS message numbers?
Note: using set_debuglevel() I can tell that smtplib believes the message to be successful, so I am fairly confident the discrepancy has something to do with the behavior of vtext numbers.
The email is being rejected because it doesn't look an email (there aren't any To From or Subject fields)
This works:
import smtplib
username = "account#gmail.com"
password = "password"
vtext = "1112223333#vtext.com"
message = "this is the message to be sent"
msg = """From: %s
To: %s
Subject: text-message
%s""" % (username, vtext, message)
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(username,password)
server.sendmail(username, vtext, msg)
server.quit()
The accepted answer didn't work for me with Python 3.3.3. I had to use MIMEText also:
import smtplib
from email.mime.text import MIMEText
username = "account#gmail.com"
password = "password"
vtext = "1112223333#vtext.com"
message = "this is the message to be sent"
msg = MIMEText("""From: %s
To: %s
Subject: text-message
%s""" % (username, vtext, message))
server = smtplib.SMTP('smtp.gmail.com',587)
# server.starttls()
server.login(username,password)
server.sendmail(username, vtext, msg.as_string())
server.quit()
I'm using the script below to send an email to myself, the script runs fine with no errors but I don't physically receive an email.
import smtplib
sender = 'foo#hotmail.com'
receivers = ['foo#hotmail.com']
message = """From: From Person <foo#hotmail.com>
To: To Person <foo#hotmail.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
EDIT
The script is named test.py
Why you use localhost as the SMTP?
If you are using hotmail you need to use hotmail account, provide the password, enter port and SMTP server etc.
Here is everything you need:
http://techblissonline.com/hotmail-pop3-and-smtp-settings/
edit:
Here is a example if you use gmail:
def mail(to, subject, text):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
Encoders.encode_base64(part)
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
I have something to add to Klark's great answer. When I try:
Encoders.encode_base64(part)
I received an error
NameError: global name 'Encoders' is not defined
It should be
encoders.encode_base64(msg)
https://docs.python.org/2/library/email-examples.html
Jeff Atwood's blog post from last April may be of some help.
The "localhost" SMTP server won't work with Hotmail. You'll have to hard-code your password in so Hotmail can authenticate you as well. The default SMTP for Hotmail is "smtp.live.com" on port 25. Try:
import smtplib
sender = 'foo#hotmail.com'
receivers = ['foo#hotmail.com']
password = 'your email password'
message = """From: From Person <foo#hotmail.com>
To: To Person <foo#hotmail.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP("smtp.live.com",25)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login(sender, password)
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"