How to send a email by using smtplib properly? - python

I wrote a code to send email using smtplib and gives me error that 'connection refused'
This is the error traceback
Traceback (most recent call last):
File "mail.py", line 20, in <module>
s = smtplib.SMTP('localhost')
File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.6/smtplib.py", line 307, in _get_socket
self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
python file
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content('Hello, This message is sent by using python
smtplib')
msg['Subject'] = 'Use of Smtp'
msg['From'] = 'sen#gmail.com'
msg['To'] = 'rec#gmail.com'
# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

if you want to send mail to gmail you have to modify gmail development settings.
refer below for more info: https://realpython.com/python-send-email/

Related

Script to Send Automated Emails via Outlook

I am attempting to set up a script that will fire off a series of emails automatically.
Below I have the code I am using to test this process.
import smtplib, ssl
username = 'me#company.com' # input('Enter username: ')
password = 'mypassword' # input('Enter password: ')
port = 443
smtp_server = "outlook.office365.com"
sender_email = username
receiver_email = "me#company.com"
message = "Hi there! This message is sent from Python."
context = ssl.create_default_context()
# s = smtplib.SMTP_SSL('outlook.office365.com')
# s.set_debuglevel(1)
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(username, password)
server.sendmail(sender_email, receiver_email, message)
When I run it, I get the following error:
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('outlook.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I want to send the email from Outlook specifically.
Checking the 'Accounts' settings I see my server is running on port 443 and has a domain of https://outlook.office365.com/subdomain/Exchange.asmx
I'm only using the outlook.office365.com portion of it since that's all that is shown in most examples I see. However I have tried using the full domain as well as just https://outlook.office365.com/. Nothing seems to work.
I have been using the info found here to guide me.
Originally I was using the domain smtp.office365.com but changed it after finding outlook.office365.com in my accounts settings. It fails with the same error anyway.
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('smtp.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I also tried with port 587 rather than 443. (443 is what I find in my outlook settings) - same error either way.
Update: I think the answer to my question is that mac's don't use SMTP...now I have to figure out how to do this in IMAP.
What protocol does Outlook for Mac use?
By default, Outlook is set to IMAP protocol. If you choose to set your account on IMAP, you may use the following settings then click Add Account.
Additional Update:
I ended up scratching this approach altogether. I ended up using an internal API service to send notifications out to all relevant parties.
For Office 365 accounts, the SMTP server name is smtp.office365.com, post 587, TLS on.

SMTP send: "getaddrinfo failed"

import smtplib
def sendEmail(receiver,subject,message):
server = smtplib.SMTP('smtp.gamil.com',587)
server.ehlo()
server.starttls()
server.login("botsinhala#gmail.com",password)
server.sendmail("botsinhala#gmail.com",receiver,message)
print("succesful")
sendEmail("gadjetrathnayake#gmail.com","testing bot","Hi, This is a testing message i'm sending to you.")
above code is generating the following error. My OS is windows 10.
Traceback (most recent call last):
File "D:/MY CHANNEL/python tricks/Email bot 1/tttttttttttttttttttttttt.py", line 17, in <module>
sendEmail("andrewhawkins#gmail.com","testing bot","Hi, This is a testing message i'm sending to you.")
File "D:/MY CHANNEL/python tricks/Email bot 1/tttttttttttttttttttttttt.py", line 5, in sendEmail
server = smtplib.SMTP('smtp.gamil.com',587)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
How can I fix this error
Replace smtp.gamil.com by smtp.gmail.com.
There are no A/AAAA records for smtp.gamil.com (2021-01-07T13:08+00:00).

Python Email Server not responding

Input
import smtplib
conn = smtplib.SMTP('imap.gmail.com',587)
conn.ehlo()
conn.starttls()
conn.login('loginmail#gmail.com', 'password')
conn.sendmail('sender1#gmail.com','sender2#gmail.com','Subject: What you like? \n\n Reply Reply Reply')
conn.quit()
Output and Error
Traceback (most recent call last):
File "E:\python\openCV\em.py", line 4, in <module>
conn = smtplib.SMTP('imap.gmail.com',587)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in _init_
(code, msg) = self.connect(host, port)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\socket.py", line 724, in create_connection
raise err
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoautError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Please help me to solve this issue. It looks like server issue. I have included the login mail and password, 2 sender emails and a subject. I want to send a zip file. The program is showing error and I have provided the error above
You are connecting to the wrong server. Should be:
conn = smtplib.SMTP('smtp.gmail.com', 587)

My python script shows the following error - OSError: [Errno 99] Cannot assign requested address

I'm creating a program to send e-mails using python. Here's my code:
import os
import smtplib
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get("xxx#gmail.com")
EMAIL_PASSWORD = os.environ.get("xxxXXxxx")
msg = EmailMessage()
msg['Subject'] = 'Python'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_ADDRESS
msg.set_content('Mail fom Python')
msg.add_alternative("""\
<!DOCTYPE html>
<html>
<body>
<h1 style="color:SlateGray;">This is an HTML Email!</h1>
</body>
</html>
""", subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
A minute after executing, I see the following in the terminal:
Traceback (most recent call last):
File "multimail.py", line 24, in <module>
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
File "/usr/local/lib/python3.7/smtplib.py", line 1031, in __init__
source_address)
File "/usr/local/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python3.7/smtplib.py", line 1037, in _get_socket
self.source_address)
File "/usr/local/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/usr/local/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
I've tried to understand the other answers to this question, but I'm a noob so can anyone help me with my specific code?

Send an email with Gmail, from Python script

I'm trying to send an email from a python script bue I get this error. I'm using a virtual server and I don't have control over the network now.
^CTraceback (most recent call last):
File "sendemail.py", line 10, in <module>
server = smtplib.SMTP('smtp.gmail.com:587')
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 566, in create_connection
sock.connect(sa)
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
The script in question, is this:
import smtplib
fromaddr = 'xpto#xpto.com'
toaddrs = 'xxx#xxx.com'
msg = 'my message'
username = 'xpto#xpto.com'
password = 'password'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Categories