WebFaction
Community site: login faq

I'm a newbie and just managed to get a python script to send email to me using this style of script (from the webfaction docs):

import sys from smtplib import SMTP

from_addr = 'my_email_address@mydomain.tld' to_addrs = ['team@mydomain.tld'] msg = open('email_msg.txt','r').read()

s = SMTP() s.connect('smtp.webfaction.com') s.login('my_mailbox_name','password1') s.sendmail(from_addr, to_addrs, msg)

Can someone tell me how I might send an attachment? I really have no idea how this is done. I plan on producing a small spreadsheet file using python and sending it as an attachment. Thanks.

asked 10 Feb '11, 23:32

darter
26134
accept rate: 14%

edited 11 Feb '11, 22:39

klynton
1.6k1210


Okay - I found a different way from a code snippet that I adapted. This actually works within webfaction and sends two attached txt files to my gmail. At this point I ran this by typing python nnn.py from my directory using putty to get access to everything. Anyway, now I know it can be done. The attachments were in the same directory as nnn.py. And now on to a glass of scotch.

import os
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

from_addr = 'qwerty@saywot.webfactional.com'
to_addrs = ['hellospam@gmail.com']
#msg="here is sample mesage"

s = smtplib.SMTP() 
s.connect('smtp.webfaction.com') 
s.login('saywot','notmyrealpassword')
s.sendmail(from_addr, to_addrs, msg)

def sendMail(to, subject, text, files=[],server="localhost"): 
assert type(to)==list 
assert type(files)==list 
from = "qwerty@saywot.webfactional.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(server)
s.sendmail(fro, to, msg.as_string() )
s.close()

sendMail( ['hellospam@gmail.com'], 
"hello","cheers there", 
["note1.txt","note2.txt"]
)
permanent link

answered 11 Feb '11, 22:04

darter
26134
accept rate: 14%

edited 11 Feb '11, 23:39

Maybe this will be formatted. Sorry about that.

import os
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

from_addr = 'qwerty@saywot.webfactional.com'
to_addrs = ['hellospam@gmail.com']
msg="here is sample mesage"

s = smtplib.SMTP() 
s.connect('smtp.webfaction.com') 
s.login('saywot','notmyrealpassword')
s.sendmail(from_addr, to_addrs, msg)

def sendMail(to, subject, text, files=[],server="localhost"): 
assert type(to)==list 
assert type(files)==list 
from = "qwerty@saywot.webfactional.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(server)
s.sendmail(fro, to, msg.as_string() )
s.close()

sendMail( ['hellospam@gmail.com'], 
"hello","cheers there", 
["note1.txt","note2.txt"]
)
(11 Feb '11, 22:07) darter

Sorry I cannot get the formatting right nor can I delete this mess. I'll try to fix this later.

(11 Feb '11, 22:11) darter

Looks great! Thanks for figuring this out and posting your solution! I'm going to edit the title so this comes up first in search results.

(11 Feb '11, 22:39) klynton

Hi,

There really isn't a good way to send an attachment. There's some docs here:

http://docs.python.org/library/email-examples.html#email-examples

but the best thing I've found is this:

http://www.velocityreviews.com/forums/t330382-how-to-add-an-attachment-with-smtplib-module.html

Where it shows exactly what you need to do to be able to send an attachment. (Warning: Not for the faint of heart.)

permanent link

answered 11 Feb '11, 01:22

klynton
1.6k1210
accept rate: 41%

Thanks for that info. I found a module that simplifies things but I;m not sure how to use it.

I have tried using the mailer module found at this link:

http://ginstrom.com/code/mailer.html

The code should be brief, just something like, according to this example:

import mailer message = mailer.Message() message.From = "me@example.com" message.To = "you@example.com" message.Subject = "My Vacation" message.Body = open("letter.txt", "rb").read() message.attach("picture.jpg") sender = mailer.Mailer('mail.example.com') sender.send(message)

But, I dont know what I should use for this line - what should I use to replace 'mail.example.com' ?

sender = mailer.Mailer('mail.example.com')

Should I put something like "smtp.webfaction.com" ?


Here are my lines I have been using without the mailer module:

from_addr = 'qwerty@saywot.webfactional.com'

to_addrs = ['hellospam@gmail.com']

msg="here is sample mesage"

s = SMTP()

s.connect('smtp.webfaction.com')

s.login('saywot','passwordnotshownhere')

s.sendmail(from_addr, to_addrs, msg)

Thanks for your response.

(11 Feb '11, 11:13) darter

Thank you klynton for fixing the formatting. I also went back and commented out one line that might have caused a problem. The line is now #msg="here is sample message" and that was just leftover from an earlier experiment. The "msg" might have caused a conflict. Also the credit belongs to the author of the snippet found here: http://snippets.dzone.com/posts/show/757. I think that my post is all ready to go for webfaction users though after they sub in their email information.

(12 Feb '11, 09:41) darter
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×261
×6

question asked: 10 Feb '11, 23:32

question was seen: 30,178 times

last updated: 12 Feb '11, 09:41

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM