So I have this Python send-email file that sends an email (the text from a file) to a recipient specified inside the code:
import smtplib
from email.mime.text import MIMEText
msgContent = open(Sourcefilelocation, "rb")
msg = MIMEText(spam.read())
msgContent.close()
msg['Subject'] = SUBJECT
msg['From'] = FROM_CLIENT_EMAIL
msg['To'] = TO_CLIENT_EMAIL
#THE ERROR BELOW
s = smtplib.SMTP('localhost')
I understand that there is no mail server on localhost, but how do I make a mail server on that and if I can't then why does s = smtplib.SMTP('gmail.com') take so long if it even works? What do I need to make it work? Any help is appreciated.
output: >> Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
s = smtplib.SMTP('gmail.com')
File "C:\Python25\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python25\lib\smtplib.py", line 310, in connect
raise socket.error, msg
error: (10013, 'Permission denied')
And the gmail smtp code takes too long.
Related
This is my code.
import smtplib
from email.message import EmailMessage
def sendemail(to, subject, message):
msg = EmailMessage()
msg.set_content(message)
msg["subject"] = subject
msg["to"] = to
user = "jibraanahmed234#gmail.com"
msg["from"] = user
password = "pmamhmifmwogjbev"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(user, password)
server.send_message("jibraanahmed234#gmail.com", "jibraanahmed10#gmail.com", msg)
server.quit()
if __name__ == '__main__':
sendemail("jibraanahmed10#gmail.com", "Hello!", "Hi Jibraan!")
This is the error it returns.
Traceback (most recent call last):
File "/Users/jibraanahmed/code/Python/messaging/sendemail.py", line 23, in <module>
sendemail("jibraanahmed10#gmail.com", "Hello!", "Hi Jibraan!")
File "/Users/jibraanahmed/code/Python/messaging/sendemail.py", line 17, in sendemail
server.send_message("jibraanahmed234#gmail.com", "jibraanahmed10#gmail.com", msg)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py",
line 939, in send_message
resent = msg.get_all('Resent-Date')
AttributeError: 'str' object has no attribute 'get_all'
These are all fake passwords and emails.
I have tried a lot of tutorials that look like they work for most people, what am I doing wrong?
Maybe you should try sendmail instead of send_message.
From the docs:
The arguments have the same meaning as for sendmail(), except that msg is a Message object
I'm trying to send an email using Python and I used the material I found on the official site (https://docs.python.org/3/library/email.examples.html) but I'm getting an error when sending an email. Using the port and server works when I use Java but when using the same details in Python the code doesn't work.
The code that I am testing:
import smtplib
from email.message import EmailMessage
port = 25
server = "172.xxx.xxx.xxx"
from_add = "test#fakemail.com"
to_add = "test#fakemail.com"
message = "This is a test Email"
subject = "Test Email"
msg = EmailMessage()
msg.set_content(message)
msg["Subject"] = subject
msg["From"] = from_add
msg["To"] = to_add
#Send the message via our own SMTP server
s = smtplib.SMTP(server)
s.send_message(msg)
s.quit()
When I run my code I get the below error. I have been searching the net and all I am finding are errors concerning sockets (I tried adding which I haven't seen in any examples. My firewall admin tells me he doesn't see any traffic being blocked his end. Does anyone know if what I am coding is wrong?
Original
Traceback (most recent call last):
File "X:/test/test_email.py", line 21, in <module>
s = smtplib.SMTP(server)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 728, in create_connection
raise err
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 711, in create_connection
sock = socket(af, socktype, proto)
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 151, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [WinError 10022] An invalid argument was supplied
Process finished with exit code 1
Socket change (didn't work)
s.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sock.bind(server, port)
I found the problem. I am using Anaconda and had created a different environment. When I switched to the root environment my code worked without a problem.
I'm trying to write a python script to send an email. And here is the code:
def main():
import smtplib
import email.mime.text
import email.mime.application
from email.mime.multipart import MIMEMultipart
from_email = 'xxx#gmail.com'
from_email_pwd = '123456'
to_email = 'yyy#gmail.com'
msg = MIMEMultipart()
msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = to_email
body = email.mime.text.MIMEText("Body")
msg.attach(body)
s = smtplib.SMTP('smtp.gmail.com')
s.starttls()
s.login(from_email, from_email_pwd)
s.sendmail(from_email, to_email, msg.as_string())
s.quit()
return
main()
While I can successfully run this code on my personal laptop, I came across problems when I try to run it on the company's desktop (working on PyCharm IDE with interpreter Python 3.5.1). Here is the error message:
Traceback (most recent call last):
File "C:/PATH/stack.py", line 29, in <module>
main()
File "C:/PATH/stack.py", line 21, in main
s = smtplib.SMTP('smtp.gmail.com')
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 306, in _get_socket
self.source_address)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 711, in create_connection
raise err
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 702, in create_connection
sock.connect(sa)
TimeoutError: [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
Process finished with exit code 1
Any thoughts what's going on?
I got the script from the first example here:
https://docs.python.org/2/library/email-examples.html#email-examples
I made sure there is no file name similar to email.py.
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = torontocoinowl#gmail.com
msg['To'] = torontocoinowl#gmail.com
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
The error is
C:\Users\donald\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/donald/PycharmProjects/untitled3/testingemail/ff.py
Traceback (most recent call last):
File "C:/Users/donald/PycharmProjects/untitled3/testingemail/ff.py", line 1, in <module>
import smtplib
File "C:\Users\donald\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 47, in <module>
import email.utils
File "C:\Users\donald\AppData\Local\Programs\Python\Python35-32\lib\email\utils.py", line 28, in <module>
import random
File "C:\Users\donald\PycharmProjects\untitled3\random.py", line 1
From random import randint
^
SyntaxError: invalid syntax
Process finished with exit code 1
You are having a file random.py in your folder. It clashes with the random modul in Python. Remove of rename that file!
I'm struggling to figure out what went wrong with the below code.
I'm trying to send html mail.
NOW = datetime.datetime.now()
def sendEmail(msg):
global NOW
global SENDER
global EMAILTARGET
today = "%s/%s/%s" % (NOW.month,NOW.day,NOW.year)
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "SAR Data Report - %s" % today
msg['From'] = SENDER
msg['To'] = EMAILTARGET
chunk = MIMEText(msg, 'html')
msg.attach(chunk)
s = smtplib.SMTP('localhost')
s.sendmail(SENDER, EMAILTARGET, msg.as_string())
s.quit()
the above code gives me the following error:
Traceback (most recent call last):
File "./html_mail.py", line 295, in <module>
sendEmail(html)
File "./html_mail.py", line 245, in sendEmail
chunk = MIMEText(msg, 'html')
File "/usr/lib/python2.7/email/mime/text.py", line 30, in __init__
self.set_payload(_text, _charset)
File "/usr/lib/python2.7/email/message.py", line 226, in set_payload
self.set_charset(charset)
File "/usr/lib/python2.7/email/message.py", line 268, in set_charset
cte(self)
File "/usr/lib/python2.7/email/encoders.py", line 73, in encode_7or8bit
orig.encode('ascii')
AttributeError: MIMEMultipart instance has no attribute 'encode'
The error in your code is that you've used msg as an in-parameter to your function and it collides with your MIME message container (both named msg).
What you need to do is to change the name of the in-parameter to something else, like html:
def sendEmail(html):
...
chunk = MIMEText(html, 'html')
...
You're passing msg, which is a MIMEMultipart object, to the MIMEText initializer, which expects a string. You should be passing a string containing the HTML you want to attach, not the message you're trying to attach it to.