473,372 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Any easy-to-use email send module?

I find that the existing email moudle is some hard for me to
understand, especially the part of how to set the CC, BCC and attach
the files. Is there any more easy one like this p-code?

import easyemail
smtpserver=easyemail.server('something')
smtpserver.login('u**@gmail.com', pwd)
newletter=smtpsever.letter(smtpserver)
newletter.sendto=['t**@1.com', 't**@2.com']
newletter.sendcc=['c**@111.com', 'c**@222.com']
newletter.sendbcc=['b***@111.com', 'b***@222.com']
newletter.body='this is the body\nline 2'
newletter.att=['c:/file1.txt', 'd:/program files/app/app.exe']

if (newletter.send()==True):
print 'send ok'
smtpserver.close()

Thanx.
Dec 20 '06 #1
2 2625
oyster wrote:
I find that the existing email moudle is some hard for me to
understand, especially the part of how to set the CC, BCC and attach
the files. Is there any more easy one like this p-code?

import easyemail
smtpserver=easyemail.server('something')
smtpserver.login('u**@gmail.com', pwd)
newletter=smtpsever.letter(smtpserver)
newletter.sendto=['t**@1.com', 't**@2.com']
newletter.sendcc=['c**@111.com', 'c**@222.com']
newletter.sendbcc=['b***@111.com', 'b***@222.com']
newletter.body='this is the body\nline 2'
newletter.att=['c:/file1.txt', 'd:/program files/app/app.exe']

if (newletter.send()==True):
print 'send ok'
smtpserver.close()

Thanx.
I'm not entirely sure where I got this code (Google search
years ago) and I've extended it a little, but you are welcome
to use it and it is very close to what you outlined above.
I had to strip out a bunch of custom logging that I include
in my version, but it think this will either work or at least
be close enough to save you some time.

-Larry
import string,sys,types,os,tempfile,time
import smtplib
import poplib
import mimetypes,mimetools,MimeWriter
class SmtpWriter:
def __init__(self, server="localhost", dest=None, src=None,
userid=None, password=None):

self.__server = server
self.__dest = dest
self.__src = src
self.__userid = userid
self.__password=password
self.__debugLevel = 0
return

def Debug(self,level):
self.__debugLevel = level
def Message(self,sender="",
subject="",
recipients=[],
body="",
attachments=[]):
if self.__debugLevel 2:
sys.stderr.write("SmtpWriter: Building RFC822 message From: %s; " \
"Subject: %s; (Length=%d with %d attachments)\n" % \
(sender, subject, len(body), len(attachments)))

sys.stderr.flush()

tempFileName = tempfile.mktemp()
tempFile = open(tempFileName,'wb')
message = MimeWriter.MimeWriter(tempFile)
message.addheader("From",sender)
message.addheader("To", reduce(lambda a,b: a + ",\n " + b, recipients))
message.addheader("Subject", subject)
message.flushheaders()
if len(attachments) == 0:
fp = message.startbody('text/plain')
fp.write(body)
else:
message.startmultipartbody('mixed')
submessage = message.nextpart()
fp = submessage.startbody('text/plain')
fp.write(body)
for attachFile in attachments:
if type(attachFile) == types.StringType:
fileName = attachFile
filePath = attachFile

elif type(attachFile) == types.TupleType and len(attachFile) == 2:
filePath, fileName = attachFile
else:
raise "Attachments Error: must be pathname string or
path,filename tuple"

submessage = message.nextpart()
submessage.addheader("Content-Disposition", "attachment;
filename=%s" % fileName)
ctype,prog = mimetypes.guess_type(fileName)
if ctype == None:
ctype = 'unknown/unknown'

if ctype == 'text/plain':
enctype = 'quoted-printable'
else:
enctype = 'base64'

submessage.addheader("Content-Transfer-Encoding",enctype)
fp = submessage.startbody(ctype)
afp = open(filePath,'rb')
mimetools.encode(afp,fp,enctype)

message.lastpart()

tempFile.close()

# properly formatted mime message should be in tmp file

tempFile = open(tempFileName,'rb')
msg = tempFile.read()
tempFile.close()
os.remove(tempFileName)
#print "about to try to create SMTPserver instance"
server=None

#
# See if I can create a smtplib.SMTP instance
#
try: server = smtplib.SMTP(self.__server)
except:
if self.__debugLevel 2:
emsg="SmtpWriter.Message-Unable to connect to " \
"SMTP server=%s" % self.__server
sys.stderr.write(emsg)
sys.stderr.flush()

raise RuntimeError(emsg)

if self.__debugLevel 2: server.set_debuglevel(1)
#
# If server requires authentication to send mail, do it here
#
if self.__userid is not None and self.__password is not None:
#
# There are two possible ways to authenticate: direct or indirect
# direct - smtp.login
# indirect - smtp after pop3 auth
#
try: response=server.login(self.__userid, self.__password)
except:
if self.__debugLevel 2:
emsg="SmtpWriter.Message-SMTP auth failed, trying POP3 " \
"auth"
sys.stderr.write(emsg)
sys.stderr.flush()

try:
M=poplib.POP3(self.__server)
M.user(self.__src)
M.pass_(self.__password)
M.stat() # Get mailbox status
M.quit()

except:
if self.__debugLevel 2:
emsg="SmtpWriter.Message-Both SMTP and POP3 auth failed " \
"email message NOT sent"
sys.stderr.write(emsg)
sys.stderr.flush()

raise RuntimeError(emsg)

#
# Send the email
#
server.sendmail(self.__src, self.__dest, msg)
if self.__debugLevel 1:
sys.stderr.write("SmptWriter.Message sent\n")
sys.stderr.flush()

server.quit()
return
if __name__ == "__main__":
import sys
import time
server="mail.domain.com"
email=SmtpWriter(server=server,
dest=["na**@domain.com.com"],
src="sm********@domain.com",
userid="sm********@domain.com",
password="password")

email.Message(sender="sm********@domain.com",
subject="Test email #2",
recipients=["me@domain.com"],
body="This is a test email from the SmtpWriter class",
attachments=["c:\\output.txt"])
Dec 20 '06 #2
ina
I put this together for some automated testing I do with an email
system. I hope it is of help to you.

It dosn't do cc and bcc In this version but it would be simple to add
to the eMessage headder.

http://phlik.ishpeck.net/index.php?P=b1114201575phlik

Dec 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Derek Wickersham | last post by:
Easy newbie question. I've searched all the PHP documentation I can find, but can't find an answer. What does the "@" mean, in this context: @mysql_select_db( "scripts");
3
by: USI Newsgroups | last post by:
First, let me apologize for how easy this probably is: DESCR TYPE SELL StartDate EndDate 65048 04 Price A 4/21/2004 4/26/2004 65048 06 Price C ...
2
by: tc | last post by:
Does anyone know where I can find an easy web page counter? -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!...
19
by: Canon EOS | last post by:
Hi, I am really new in .net and pocket PC development. My background are purely C/C++/VC++. Have developed on Mobile Java for a year and felt completely insecure with it because all codes can...
7
by: Tim | last post by:
hi all, I have a table of customers. I have a table of products they have ordered. How can I find all customers who have ordered productA and productB at any time. It sounds so easy, but...
0
by: Wong chian | last post by:
Hi, I have this easy piece of code public class Box1:System.web.ui.webcontrols.webcontro protected Button mybutton protected override void OnInit( InitializeComponent()
4
by: Wong chian | last post by:
Hi, I have this easy piece of code: public class Box1:System.web.ui.webcontrols.webcontrol { protected Button mybutton; protected override void OnInit() { InitializeComponent();
3
by: Eifel-benz | last post by:
I'm working the first days with Visual C#.net. So most of you think these questions are really easy. 1. How can I read out the windows-account? 2. How can I read datas from an INI-file? A...
6
by: Tim Geiges | last post by:
I am trying to open an image in a little image viewer I wrote(it has its own file explorer but I want it to also open from Windows Explorer) When I select an image from widows explorer and use...
4
by: Dino M. Buljubasic | last post by:
I dont know much about threads, so please be easy on explaining me this one. How do I stop a thread completelly (kill it)? What happens with a thread that has been started but not killed? ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.