sending email with smtp (help needed) - python

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()

Related

Email send but not received in python

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")

Trying to send email from python

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"

Gmail SMTP rejecting my login

smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvuQ\n5.7.14 hjav3RshZ9XqmApuN6mVTPJ_3AZUJEkiniSxdgdVMrgEpKpUtHi8_oCjzuOA9pkhGMyTrs\n5.7.14 fuSX9EuvWudU00Q1KXZgY4rZ1I5ZEEDOqvVMl7bOQitwyb_sYdgPA3tJC7_xpUN1zDC6Ib\n5.7.14 MjA2mM_oMdCOeCpodh-13LwLFlyzmZALwg2uu522OxG0NH74B2hafBfT2F1XK0lXCz1hce\n5.7.14 3yugD0g> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 bw2sm40059670pad.46 - gsmtp')
I am getting the above error from the below script. And yes I have verified that I am using my correct credentials. All I want to do is send an email from a script! Has anyone run into this issue before?
import smtplib
FROMADDR = "my.real.address#gmail.com"
LOGIN = FROMADDR
PASSWORD = "my.real.password"
TOADDRS = ["my.real.address#gmail.com"]
SUBJECT = "Test"
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
% (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
You have to enable 'Access for less secure apps' option in your Google account security section.
Try changing your code to:
server.ehlo()
server.starttls()
server.ehlo() # you are missing elho to establish communication with server
server.login(username, password)
# Full script
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from_address = 'you#gmail.com'
to_address = 'you80#gmail.com'
text = 'test message sent from Python'
username = '****'
password = '****'
msg = MIMEMultipart()
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = 'Foo'
msg.attach(MIMEText(text))
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(from_address, to_address, msg.as_string())
server.quit()
If you have not logged in using your browser since the error messages, you should also try that as if you had a number of unsuccessful logins you will have to enter a captcha,
The solution to this ended up being changing my gmail password. I never did figure out which special characters were throwing everything off, but I just generated a new password and had no problems with this after that.

Sending Verizon SMS message via Python and smtplib

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()

Python: Email problem

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"

Categories

Resources