I'm using python 2.7.3. and I have following code for send emails with attached file.
# coding: cp1251
import os
import smtplib
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.Utils import formatdate
def sendEmail(to_address, mail_settings, attachment_file_path, subject = 'my_subject'):
HOST = mail_settings['host']
port = mail_settings['port']
username = mail_settings['login']
password = mail_settings['pass']
msg = MIMEMultipart()
msg["From"] = mail_settings['from_address']
msg["To"] = ','.join(to_address)
msg["Subject"] = subject.decode('cp1251')
msg['Date'] = formatdate(localtime=True)
msg['Content-Type'] = "text/html; charset=cp1251"
# attach a file
part = MIMEBase('application', "octet-stream")
part.set_payload( open(attachment_file_path,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attachment_file_path))
msg.attach(part)
server = smtplib.SMTP(host=HOST, port=port)
try:
failed = server.sendmail(mail_settings['from_address'], to_address, msg.as_string())
print('sent')
server.close()
except Exception, e:
errorMsg = "Unable to send email. Error: %s" % str(e)
print(errorMsg)
My problem is that exchange users who receive emails through this code can't see attachment file name if it has russian letters (for example пример.txt), otherwise if it has english letters everything works fine for them.
I faced with this problem only with customers who use exchange (gmail works fine).
What i'm doing wrong? where should I change encoding?
I found the solution finally. I just set encoding for header.
mail_coding = 'utf-8'
att_header = Header(os.path.basename(attachment_file_path), mail_coding);
part.add_header('Content-Disposition', 'attachment; filename="%s"' % att_header)
Came here with the same problem and Savva Sergey's own solution didn't work for me.
Encoding with os.path.basename(email_attach1).encode('utf-8') was useless too.
And I'm positive that's happened because of python's version. Mine is 3.8 and while I'm doing almost the same, here it is what's works:
import os
from email.mime.application import MIMEApplication
email_attach1 = './path/to/文件.pdf'
part = MIMEApplication(
open(email_attach1,"rb").read(),
Name=os.path.basename(email_attach1),_subtype="pdf"
)
part.add_header('Content-Disposition',
'attachment',filename=os.path.basename(email_attach1))
Related
I have been trying to send a mail via python with a Docx attachment
I'm able to get a mail but with no attachment to it, below is the code I used for attaching a file
And I didn't get any error while attaching a file
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
sender_email = email
receiver_email = toemail
password = password
message = MIMEMultipart("alternative")
message["Subject"] = Subject
message["From"] = sender_email
message["To"] = receiver_email
message.attach(htmlmessage)
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octet-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)
#encode the attachment
#add payload header with filename
payload.add_header('Content-Disposition', "attachment; filename= %s" % attach_file)
message.attach(payload)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)
Mime types are to be respected. The type multipart/alternative should be used for messages containing the same information in plain text and in HTML. The mail reader can then choose which representation it can use.
On the other hand, multipart/mixed should be used when the message contains multiple different parts, said differently to transport attachements.
So, provided htmlmessage is a valid email.mime.text.MIMEText, your code should not declare "alternative":
...
message = MIMEMultipart()
...
Furthermore, you should avoid to directly use MIMEBase and instead rely on the defaults for MIMEApplication:
payload = MIMEApplication(attach_file.read(),
'vnd.openxmlformats-officedocument.wordprocessingml.document')
payload.add_header('Content-Disposition',
"attachment; filename= %s" % attach_file)
message.attach(payload)
But I must acknowledge that this last point is mainly a matter of taste...
I am trying to send mail using python script. I tried without attachment it is working.
Now i tried with attachment i am getting assertion error.
Below is the code:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import time
import random
msg_from = "xyz#abc.com"
to = "xyz#abc.com"
text = "test-Hello"
subject = "Test"
f = "output1.pdf"
def generate_message_id(msg_from):
domain = msg_from.split("#")[1]
r = "%s.%s" % (time.time(), random.randint(0, 100))
mid = "<%s#%s>" % (r, domain)
return mid
def send_mail(msg_from, to, subject, text,
files=[],server="10.10.10.10", debug=False):
assert type(to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = msg_from
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
text = text.encode("utf-8")
text = MIMEText(text, 'plain', "utf-8")
msg.attach(text)
msg.add_header('Message-ID', generate_message_id(msg_from))
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
if not debug:
smtp = smtplib.SMTP(server)
smtp.sendmail(msg_from, to, msg.as_string())
smtp.close()
return msg
send_mail(msg_from, to, subject, text,files=[],server="10.10.10.10", debug=False)
Error i am getting:
Traceback (most recent call last):
File "testmail1.py", line 53, in <module>
send_mail(msg_from, to, subject, text,files=[],server="10.10.10.10", debug=False)
File "testmail1.py", line 24, in send_mail
assert type(to)==list
AssertionError
I am using from linux os and using python 2.7
Please help me to fix
Your to field should be a list, because you could theoretically send your email to multiple persons. So I'd suggest:
to = ["xyz#abc.com"]
The send_mail method checks first if your to field is a list, that's why you're getting the error.
Also check out the documentation
I need to figure out how to make a script that scans for new files in a directory, and when there is a new one, sends the file via email.
Someone keeps stealing bikes in my apartment building! First it was my fault (I for got to lock it), now the crook upgraded by cutting chains. I had it after the crook stole my second bike by cutting 1/2 inch airplane wire.
Anyway, using a raspberry pi as a motion activated security camera, I want it to send me a video file as soon as the video program finishes recording it. This is incase they steal the pi.
I am looking at these examples, but I can't figure how to make the script run continuously (every minute) or how to make it scan a folder for a new file.
How do I send attachments using SMTP?
OK
I got it down to scanning and then trying to email. It fails when trying to attach the video file. Can you help? Here is the revised code:
The failure is:
msg = MIMEMultipart()
TypeError: 'LazyImporter' object is not callable, line 38
import time, glob
import smtplib
import email.MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders, MIMEMultipart
import os
#Settings:
fromemail= "Jose Garcia <somerandomemail#gmail.com>"
loginname="somerandomemail#gmail.com"
loginpassword="somerandomepassword"
toemail= "Jose Garcia <somerandomemail#gmail.com>"
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation="/Users/someone/Desktop/Test/*.mp4"
subject="Security Notification"
def mainloop():
files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
while 1:
new_files=glob.glob(fileslocation)
if len(new_files)>len(files):
for x in new_files:
if x in files:
print("New file detected "+x)
print("about to call send email")
sendMail(loginname, loginpassword, toemail, fromemail, subject, gethtmlcode(), x, SMTPserver, SMTPort)
files=new_files
time.sleep(1)
def sendMail(login, password, to, frome, subject, text, filee, server, port):
# assert type(to)==list
# assert type(filee)==list
msg = MIMEMultipart()
msg['From'] = frome
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
# #for filee in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(filee,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(filee))
msg.attach(part)
smtp = smtplib.SMTP(SMTPserver, SMTPort)
smtp.sendmail(frome, to, msg.as_string() )
server.set_debuglevel(1)
server.starttls()
server.ehlo()
server.login(login, password)
server.sendmail(frome, to, msg)
server.quit()
def gethtmlcode():
print("about to assemble html")
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
html +='"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
html +='<body style="font-size:12px;font-family:Verdana"><p>A new video file has been recorded </p>'
html += "</body></html>"
return(html)
#Execute loop
mainloop()
I finally got it working. I had to use python 2.5 to get rid of the LazyImporter error. Every time a new file is added to the security folder, it gets emailed to me. Logging is broken.
import time, glob
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEMultipart import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
import datetime
from email import Encoders
import os
import sys
#Settings:
fromemail= 'RaspberryPI Security Camera <someone>'
loginname='someone#gmail.com'
loginpassword='something'
toemail= 'Jose Garcia < someone#gmail.com>'
SMTPserver='smtp.gmail.com'
SMTPort=587
fileslocation='/Users/someone/Desktop/Test/*.*'
subject="Security Notification"
log='logfile.txt'
def mainloop():
files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
while 1:
f = open(log, 'w')
sys.stdout = Tee(sys.stdout, f)
new_files=glob.glob(fileslocation)
if len(new_files)>len(files):
for x in new_files:
if x in files:
print(str(datetime.datetime.now()) + "New file detected "+x)
sendMail(loginname, loginpassword, toemail, fromemail, subject, gettext(), x, SMTPserver, SMTPort)
files=new_files
f.close()
time.sleep(1)
def sendMail(login, password, send_to, send_from, subject, text, send_file, server, port):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
part = MIMEBase('application', "octet-stream")
part.set_payload( open(send_file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(send_file))
msg.attach(part)
smtp = smtplib.SMTP(SMTPserver, SMTPort)
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.starttls()
smtp.login(login, password)
smtp.sendmail(send_from, send_to, msg.as_string() )
smtp.close()
def gettext():
text = "A new file has been added to the security footage folder. \nTime Stamp: "+ str(datetime.datetime.now())
return(text)
class Tee(object):
def __init__(self, *files):
self.files = files
def write(self, obj):
for f in self.files:
f.write(obj)
#Execute loop
mainloop()
It looks like the email module has been refactored over time. This fixed the 'LazyImporter' object not callable error for me on Python 2.7:
from email.mime.text import MIMEText
Noteably it was not happy with (what I thought were) synonyms like import email; email.mime.text.MIMEText(...)
I use python3 and I could not for the life of me get any of these examples to work, but I was able to come up with something that works and is a whole lot simpler.
import smtplib
from email.message import EmailMessage
from email.mime.base import MIMEBase
from email import encoders
# Defining Objects and Importing Files ------------------------------------
# Initializing video object
video_file = MIMEBase('application', "octet-stream")
# Importing video file
video_file.set_payload(open('video.mkv', "rb").read())
# Encoding video for attaching to the email
encoders.encode_base64(video_file)
# creating EmailMessage object
msg = EmailMessage()
# Loading message information ---------------------------------------------
msg['From'] = "person_sending#gmail.com"
msg['To'] = "person_receiving#gmail.com"
msg['Subject'] = 'text for the subject line'
msg.set_content('text that will be in the email body.')
msg.add_attachment(video_file, filename="video.mkv")
# Start SMTP Server and sending the email ---------------------------------
server=smtplib.SMTP_SSL('smtp.gmail.com',465)
server.login("person_sending#gmail.com", "some-clever-password")
server.send_message(msg)
server.quit()
Just put your script in a loop and have it sleep for 60 seconds. You can use glob to get a list of files in the directory. in is pretty useful for seeing what is in a list (i.e. the list of files in the directory).
import time, glob
files=glob.glob("/home/me/Desktop/*.mp4") #Put whatever path and file format you're using in there.
while 1:
new_files=glob.glob("/home/me/Desktop/*.mp4")
if len(new_files)>len(files):
for x in new_files:
if x not in files:
print("New file: "+x) #This is where you would email it. Let me know if you need help figuring that out.
files=new_files
time.sleep(60)
so i have this code:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
def sendMail(to, subject, text, files=[],server="smtp.gmail.com:587"):
assert type(to)==list
assert type(files)==list
fro = "psaoflamand#live.com>"
msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
a=0
username = 'psaoflamand#gmail.com'
password = 'pass'
# The actual mail send
smtp = smtplib.SMTP(server)
smtp.starttls()
smtp.login(username,password)
for file in files:
a+=1
print a
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
if a==21:
smtp.sendmail(fro, to, msg.as_string() )
a=0
print 'sent'
smtp.quit()
sendMail(
["psaoflamand#live.com"],
"hello","cheers",
["Thousands of one megabyte files"]
in this code it sends 21 files at a time to avoid going over the limit of gmail messages. But the probleme is that the data in MIMEBase stays... my question is is there a way to delete all data in the MIMEBase? Im sorry that the indentation is wrong
It looks like your problem is that you:
Create a msg.
Append 21 files to msg.
Send it.
Append 21 more files, so that it has 42 files now attached.
Send it again; this second message is twice as large as the first.
Append 21 more files, bringing the total to 63.
Send it again; it's getting pretty huge now.
And so forth.
When a==21 you should start over with a fresh msg object instead of continuing to append more and more files to the old one.
Alternately, you could try removing the 21 attachments already there before attaching the new ones; but just starting over might be simpler, since you already have the code in place to start a new message with the right headers — it just needs refactoring into a little “start a new message” function.
I am trying to send mail using Python 3.2. My code is as follows:
#from email import Encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
import os
import smtplib
from base64 import encode
from email import encoders
def sendMail(to, subject, text, files=[],server="smtp.mydomain.com"):
assert type(to)==list
assert type(files)==list
fro = "From <myemail#mydomain.com>"
msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
smtp = smtplib.SMTP_SSL(server, 465)
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.login("myemail#mydomain.com", "mypassword")
smtp.ehlo()
smtp.sendmail(fro, to, msg.as_string() )
smtp.close()
print("Email send successfully.")
sendMail(
["recipient"],
"hello","cheers",
[]
)
It gives me following error:
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (501, b'5.7.1 <myemail#mydomain.com>... Permission denied', 'myemail#mydomain.com')
Does anybody know how to solve this problem?
Thanks in advance.
As the error says: you need to call the connect method on the smtplib.SMTP_SSL instance before you try to use it. smtplib.SMTP_SSL does not automatically connect (and neither does smtplib.SMTP.)