Here is the code I am using to send email through GoDaddy:
import smtplib
server = smtplib.SMTP('smtpout.secureserver.net', 465)
server.starttls()
server.ehlo()
server.login("username", "password")
msg = "Please work!!!!!!"
fromaddr = "fromemail"
toaddr = "toemail"
server.sendmail(fromaddr, toaddr, msg)
When running the script, I get this error:
Traceback (most recent call last):
File "emailTest.py", line 3, in <module>
server = smtplib.SMTP('smtpout.secureserver.net', 465)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 362, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
I'm really lost on this one, and I know for a fact that my login information is correct.
If anyone is having problems with RFC 5322 compliant emails this ended up working for me:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg.set_unixfrom('author')
msg['From'] = 'you#mail.com'
msg['To'] = 'them#mail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
mailserver.ehlo()
mailserver.login('yourgodaddy#mail.com', 'PASSWORD')
mailserver.sendmail('you#mail.com','them#mail.com',msg.as_string())
mailserver.quit()
Replace these two lines:
server = smtplib.SMTP('smtpout.secureserver.net', 465)
server.starttls()
with these two:
server = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
#server.starttls()
Quoting the doc:
SMTP_SSL should be used for situations where SSL is required from the beginning of the connection and using starttls() is not appropriate.
Using port 465 is one of those situations. SMTP.starttls() is appropriate when you use port 25 or port 587.
References:
http://en.wikipedia.org/wiki/SMTPS
https://www.fastmail.fm/help/technical/ssltlsstarttls.html
Joe's code worked for me. The only thing i had to change were the import statements:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Then everything worked! Thanks, Joe.
(Sorry I couldn't leave this as a comment or upvote it - my reputation isn't high enough to comment yet.)
Here is the overall code that works.
import smtplib`enter code here`
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg.set_unixfrom('author')
msg['From'] = 'your email'
msg['To'] = 'xyz#mail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
mailserver.ehlo()
mailserver.login('your email', 'password')
response = mailserver.sendmail('from#mail.com','to#mail.com',msg.as_string())
mailserver.quit()
Related
So I set up a GCP VM with Ubuntu, from where I want to send regular reports through my mail provider with a python script. The smtp port is 587, and to my understanding the port was formerly closed in GCP environments but should now be available.
My script looks like this:
import smtplib,ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import time
timestr = time.strftime('(%Y-%m-%d)')
username='me#mydomain.de'
password='secretpw'
send_from = 'me#mydomain.de'
send_to = 'recipient#herdomain.com'
body = 'hello'
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = 'important email'
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.ionos.de')
port = '587'
smtp = smtplib.SMTP('smtp.ionos.de')
smtp.ehlo()
smtp.starttls()
smtp.login(username,password)
smtp.sendmail(send_from, send_to.split(','), msg.as_string())
smtp.quit()
On execution, the machine takes some time before outputting a timeout:
Traceback (most recent call last):
File "test.py", line 25, in <module>
server = smtplib.SMTP('smtp.ionos.de')
File "/usr/lib/python3.8/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
I can, however, ping smtp.ionos.de as well as telnet smtp.ionos.de 587 form the cl with result of a working ping and connection.
I also tried this with other email providers including gmail and get stuck with the exact same outcome.
Anyone? Help appreciated, thanks.
Your code has multiple problems:
Connecting to the server twice.
Not specifying the port number when connecting
Not creating an SSL context for encryption.
In your code, replace these lines:
server = smtplib.SMTP('smtp.ionos.de')
port = '587'
smtp = smtplib.SMTP('smtp.ionos.de')
smtp.ehlo()
smtp.starttls()
With:
context = ssl.create_default_context()
smtp = smtplib.SMTP('smtp.ionos.de', 587)
smtp.ehlo()
smtp.starttls(context=context)
Here is a full example tested with a GCP Cloud Function. No libs are needed in requirements.txt. This uses Python 3.9. The code uses an App Password because normal users require 2FA.
import smtplib
import ssl
def send_email(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
gmail_user = 'user#gmail.com'
gmail_password = 'YOURPASSWORD'
sent_from = gmail_user
to = ['foo#bar.com']
subject = 'Test e-mail from Python'
body = 'Test e-mail body'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
context = ssl.create_default_context()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls(context=context)
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, body)
server.close()
print('Email sent (print)!')
return f'Email sent (return)!'
I tried with "allow less secure app" and "unlockCaptcha", but still not work.
How to know if the problem is with my account or with the code?
Thanks for attention, here teh code:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# create message object instance
msg = MIMEMultipart()
message = "Prova"
# setup the parameters of the message
password="pass"
msg['From'] = "senders#example.com"
msg['To'] = "receiver#example.com"
msg['Subject'] = "Prova"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
#create server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(msg['From'], password)
# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print(f"successfully sent email to {msg['To']}")
I have written a python script to send email from my gmail account to my another email. the code is:
import smtplib
fromaddr = 'my#gmail.com'
toaddrs = 'to#gmail.com'
msg = 'my message'
username = 'my#gmail.com'
password = 'myPass'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print("Done")
I'm also getting the "Done" output as the email is being sent. But the problem is: I can't seem to receive the email. It doesn't show up in the inbox. I can't find the problem :( Can anyone please help?
Thanks in advance...
Check this out, this works for me perfectly...
def send_mail(self):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
gmailUser = 'myemail#gmail.com'
gmailPassword = 'P#ssw0rd'
recipient = 'sendto#gmail.com'
message='your message here '
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = "Subject of the email"
msg.attach(MIMEText(message))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
Enable less secure apps on your gmail account and for Python3 use:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
gmailUser = 'XXXXX#gmail.com'
gmailPassword = 'XXXXX'
recipient = 'XXXXX#gmail.com'
message = f"""
Your message here...
"""
msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Your Subject..."
msg.attach(MIMEText(message))
try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print ('Email sent!')
except:
print ('Something went wrong...')
my question is whether I can send an email via python in a c++ program.
That is my actual python script.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
# From
fromaddr = "...#gmail.com"
# To
toaddr = "..."
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
# subject
msg['Subject'] = "..."
# Text
body = "..."
msg.attach(MIMEText(body, 'plain'))
#smtplib import
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("username", "password")
text = msg.as_string()
#send email
server.sendmail(fromaddr, toaddr, text)
My plan ist it, that I will open python and import the script via the commandline in the programm.
Is there a better way to do it?
The problem is that c++ open via system("python") the commandline, but now I have to write import mail.py in the command line.
Can I do this automaticly with an other order? I will open python, import mail and quit with one order. Is that possible?
Thank You!
Here is the program for an email.
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(email, password)
smtpserver.sendmail(email, recipient, text)
print 'Message Sent!'
smtpserver.close()
The variables: email is your email address, password is the password to your email, recipient is the receiver of the email, and text is the text to send.
I HAVE A LITTLE PROBLEM.
I USE:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'me#gmail.com'
msg['To'] = 'you#gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('me#gmail.com', 'mypassword')
mailserver.sendmail('me#gmail.com','you#gmail.com',msg.as_string())
mailserver.quit()
AND EVERTHING IS OKAY - BUT I WOULD LIKE TO ADD A TXT FILE ATTACHMENT. CAN YOU HELP ME?
You could achieve it like this:
filename = ...
with open(filename,'r') as f:
message = MIMEText(f.read())
message.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(message)
where msg is the MIMEMultiPart object.
I FIND ANSWER TO BUT THANKS A LOT ! :)
filename='/www/pages/DANE/komunikaty.txt'
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="txt")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)