Send an email with Gmail, from Python script - python

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

Related

socket.gaierror: [Errno 11001] getaddrinfo failed when sending gmail email via Python

I'm on Windows and using Python 3.9.10 and I have trouble sending an email via google smtp.
My code is :
import smtplib
from email.mime.text import MIMEText
def send_email(host, port, subject, msg, sender, recipients, password):
msg = MIMEText(msg)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
smtp_server = smtplib.SMTP_SSL(host, port)
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
smtp_server.quit()
def main():
host = "smtp.gmail.com"
port = 465
user = <my email>
pwd = <google app password>
subject = "Test email"
msg = "Hello world"
sender = user
recipients = [user]
send_email(host, port, subject, msg, sender, recipients, pwd)
if __name__ == '__main__':
main()
What I don't understand is I keep getting the error socket.gaierror: [Errno 11001] getaddrinfo failed:
Traceback (most recent call last):
File "%project_location%\send_email_google.py", line 37, in <module>
main()
File "%project_location%\send_email_google.py", line 33, in main
send_email(host, port, subject, msg, sender, recipients, pwd)
File "%project_location%\send_email_google.py", line 13, in send_email
smtp_server = smtplib.SMTP_SSL(host, port)
File "%Python%\lib\smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "%Python%\lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "%Python%\lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "%Python%\lib\smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "%Python%\lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "%Python%\lib\socket.py", line 823, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "%Python%\lib\socket.py", line 954, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
Process finished with exit code 1
I'm not using a VPN, proxies or anything.
I have checked, I have the correct host and the correct port for Gmail. I'm using the google app password thing. I just don't get why it won't work.
Thanks!
I fixed it
I had a typo and my host was not "smtp.gmail.com" but was "'smtp.gmail.com'"

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

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?

How to send a email by using smtplib properly?

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/

Categories