I have written the following code to send an email that simply sends "test" to a specified email address.
import smtplib
sentFrom = "my email"
to = input("Enter email: ")
#numberEmails = raw_input ("Enter number of emails to send")
messageText = "test"
msg = "From: %s\n To: %s\n\n%s" % (sentFrom, to, messageText)
username = str("my email")
password = str("my password")
try :
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(sentFrom, to, msg)
server.quit()
print (" Email has sent")
except :
print("email NOT sent")
When I try to send more emails than 1, for example:
5*server.sendmail(sentFrom, to, msg)
Only one email sends, then I get the error "email NOT sent." I do not get an "email has sent" after the first email sends, only an "email NOT sent" print after the first. How can I fix this issue? I want to eventually have an input() so I can enter in the number of emails to send, and the program sends the number of emails entered into the input. I put how I think this would look as a comment at the top. Can I just multiply the server.sendmail command by the numberEmails command?
This is my first day using Python so go easy on me pls. :)
You can't multiply this.
Multiplications can only be done with numbers, server.sendmail(sentFrom, to, msg) does not result in a number.
You're going to have to loop through the amount needed.
So as an example, this prints "hello world" five times:
def test():
print "hello world"
numberEmails = 5
for _ in range(numberEmails):
test()
Off course you have to parse your numberEmails to int if it's not an integer.
Eventually you have to loop through this entire block 5 times
try :
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(sentFrom, to, msg)
server.quit()
print (" Email has sent")
except :
print("email NOT sent")
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")
I'm trying to check whether an email exists or not using Python's smtplib
This is what I did:
s = smtplib.SMTP()
s.connect(mxRecord)
s.mail('my#email.com') //Here the error shows up
The error is:
Client host [...] blocked using Spamhaus. To request removal from this list see http://www.spamhaus.org/lookup.lasso (S3130)
I tried something and it worked well.
import dns.resolver, smtplib
MyEmail = "X#hotmail.com"
MyPassword = "XXX"
EmailToValidate = "X#Y.com"
record = dns.resolver.query(str.split(EmailToValidate, "#")[1], "MX")
mx = str(record[0].exchange)
server = smtplib.SMTP("smtp.live.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(MyEmail, MyPassword)
server.helo("live.com")
server.connect(mx)
server.mail(MyEmail)
code, msg = server.rcpt(EmailToValidate)
print("Code: ", str(code), " message: ", msg)
the (code, message) pair will be (250, OK) if the email exists, and (550, Address Rejected) if the email does not exists.
I wrote this on hurry, so there might be some unnecessary steps.
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"
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()