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?
Related
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.
I am working with a list that consists of servers (Name, IP) tuples. I am testing each server for connectivity using ping. If the ping fails, it's added to a list called issues. I am attempting to email this list of failures to myself, given there are any. I'm not sure what I've got wrong, but I'm getting the following error:
Traceback (most recent call last):
File "C:/Python27/Scripts/serverconnection.py", line 26, in <module>
msg['SUBJECT'] = "Server Disconnect Notice"
TypeError: 'str' object does not support item assignment
The code generating the error is below
if len(issues) > 0 :
body = '\n'.join('%s, %s' % server for server in issues)
msg = body
msg['SUBJECT'] = "Server Disconnect Notice"
msg['FROM'] = "Alli Deacon"
msg['TO'] = 'allid#atlanticpkg.com'
msg.attach(text)
Because msg is a str, so you can't do msg['SUBJECT'] as in dict
Try the following:
msg = dict()
msg['SUBJECT'] = "Server Disconnect Notice"
msg['FROM'] = "Alli Deacon"
msg['TO'] = 'allid#atlanticpkg.com'
msg.send() # i don't know what module for e-mails you use
I needed the following import:
from email.MIMEText import MIMEText
And then updated my code with the following:
msg = MIMEText(body)
After I updated with these additions, the code worked fine.
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
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.
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()