I have tried to use flask_mail to send emails via gmail SMTP. I want to simply send an email back to the host with some details.
I have set the following settings
app = Flask(__name__)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'fakeemail#gmail.com'
app.config['MAIL_PASSWORD'] = 'fakepassword'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
And wrote the following code to send
def send_email(senders_email, senders_subject, senders_feedback):
print("email " + senders_email)
print("sub " + senders_subject)
print("feed " + senders_feedback)
msg = Message('Feedback from ' + senders_email, sender='fakeemail#gmail.com',
recipients=['fakeemail#gmail.com'])
print("message defined")
msg.body = "Users Subject: " + senders_subject + "\n" + "Users Feedback: " + senders_feedback
print("body set")
mail.send(msg)
print("message sent")
At first I was getting successful emails which sent the specific email to me but now I get a 500 error after about 20 or 30 seconds
OSError: [Errno 101] Network is unreachable
Any help would be appreciated
I came across a quite similar problem and it turned out the problem is caused by the mail port you are using (465). The Bluehost has blocked this port to discourage spam. Detailed information is available with the link:
https://my.bluehost.com/cgi/help/500
It seems that maybe you either need to buy their service (which might still not be working, because the port is also blocked for a dedicated IP) or try to find a detour. In my own case, I changed SMTP to the E-mail address I use at the university since the website is just an intern thing.
Related
Using my gmail account as a test for sending mail using python. It appears the mail is getting sent (or at least passes over the mail server) but it is not being received. I cannot seem to find any logs that would allow me to trace the email (Using Linux Mint 20). First question is where can I find these logs? Secondly, does anything in this script seem incorrect? Please assume that the data is correctly populated. My gmail account is set to allow less secure apps as well.
with open("config.json",) as config:
data = json.load(config)
smtp_server = data["MAIL"]["SMTP_SERVER"]
port = data["MAIL"]["SMTP_PORT"]
sender_email = data["MAIL"]["SMTP_ACCOUNT"]
password = data["MAIL"]["SMTP_PASSWORD"]
context = ssl.create_default_context()
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(sender_email,password)
return server
except Exception as e:
print(e)
return None
i was making a simple daemon in python which takes a mail queue and delivers them to the recipients. Everything is working pretty good except from the smtplib which is actually the most important part.
What happens?
When im running the script im getting the following error:
root#vagrant-ubuntu-trusty-64:/mailer/tests# python daemon_run.py
[Errno -2] Name or service not known
From what i found on the internet this error occurs when it cant connect to the SMTP server. Most users suggested fixes on postman which i dont use since i take advantage of google's services.
The code
headers = "\r\n".join(["from: " + "my_email#gmail.com",
"subject: " + "Testing",
"to: " + "recipient#gmail.com",
"mime-version: 1.0",
"content-type: text/html"])
content = headers + "\r\n\r\n" + template_content
server = smtplib.SMTP('smtp.google.com', 587)
server.ehlo()
server.starttls()
server.login('my_email#gmail.com', 'pass')
server.sendmail('my_email#gmail.com', 'recipient#gmail.com', content)
server.close()
Please note that i'm using exactly the same login details in PHPMailer which actually works.
Any ideas?
It seems like the goold old typo hit again. Gmail's SMTP is smtp.gmail.com and not smtp.google.com
I want my Python script to send me mail me when there is any exception. I have tried some code related to SMTP that I found, but unfortunately it's not executing and showing an error. Please help me to find out the main problem.
The SMTP code:
import smtplib
import string
SERVER = 'localhost'
SUBJECT = "Test email from Python"
TO = "abc.def#defghij.com"
FROM = "python#mydomain.com"
text = "Sample of mail"
BODY = string.join(("From: %s" % FROM,"To: %s" % TO,"Subject: %s" %SUBJECT ,"",text), "\r\n")
server = smtplib.SMTP(SERVER) #Here in this line it showing error
server.sendmail(FROM,TO,BODY)
server.quit()
** error message * server = smtplib.SMTP(SERVER)
socket.error: (10047, 'Address family not supported by protocol family: See http://wiki.python.org/jython/NewSocketModule#IPV6addresssupport')
I tested this code with Jython 2.5.3 and Python 2.7 and it works if I change SERVER from localhost to real SMTP server. I took SMTP server address from my email client configuration.
You can try this with yagmail, it will make it all much more convenient:
from yagmail import SMTP
SMTP(FROM).send(TO, SUBJECT, text)
It really requires only that much, and if you do some setup you're registered and safe (no need to write your username and password in script).
Read more about it at github.
I'm attempting to send an email with Python. From what I can tell, all the following code needs is a valid recipient and a host HOST. I'm unsure on how to get the host. What is the simplest way?
import smtplib
import string
SUBJECT = "Test email from Python"
TO = "python#mydomain.com"
FROM = "python#mydomain.com"
text = "blah blah blah"
BODY = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"",
text
), "\r\n")
server = smtplib.SMTP(HOST)
server.sendmail(FROM, [TO], BODY)
server.quit()
The HOST is the SMTP relay provided by your ISP (typically related to the domain name of your from address). If you use a desktop mail client, you'll be able to see the SMTP server listed in your mail settings. If you're hosting using shared hosting, your hosting provider should be able to provide an SMTP server for you to use.
We are not in the position to tell you a valid mail host aka smtp server. You should know where to send your mail to: either ask your organization IT or ask your mail provider.
I have the following code
import smtplib
from email.mime.text import MIMEText
smtpserver = 'smtp.gmail.com'
AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
smtpuser = 'admin#myhost.com' # for SMTP AUTH, set SMTP username here
smtppass = '123456' # for SMTP AUTH, set SMTP password here
RECIPIENTS = ['online8#gmail.com']
SENDER = 'admin#myhost.com'
msg = MIMEText('dsdsdsdsds\n')
msg['Subject'] = 'The contents of iii'
msg['From'] = 'admin#myhost.com'
msg['To'] = ''online8#gmail.com''
mailServer = smtplib.SMTP('smtp.gmail.com',587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(smtpuser, smtppass)
mailServer.sendmail(smtpuser,RECIPIENTS,msg.as_string())
mailServer.close()
this code works fine on my desktop. but it failed with this error
smtplib.SMTPAuthenticationError: (535, '5.7.1 Username and Password not accepted. Learn more at\n5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 21sm4713429agd.11')
on my linux server.
Not sure what went wrong, should i open some port on my linux server?
Port 587 obviously needs to be open, but it probably is (or you wouldn't have gotten the detailed error msg in question). Python 2.5 vs 2.6 should make no difference. I think the issue has to do with "solving a captcha" once on the computer for which logins are currently getting rejected; follow the detailed instructions at the URL in the error message, i.e., http://mail.google.com/support/bin/answer.py?answer=14257
import random,time
for i in range(1,100):
y=random.randint(30,300)
time.sleep(y)
print ("Mailing for fun, Mail No: " + str(i))
msg = MIMEText('Testing mailing \n Mail No:' + str(i))
msg['Subject'] = 'Mail Number: ' + str(i)
Randomizing the mail interval to check smtp behavior :)
With a bit addition n modification, I got this to work to check our intermittent mail bouncing.