Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Email in Bash

//Bash email
#!/bin/bash

function email
{
echo "this is a test" | mail -s "test" "$1"
}

if [ "$1" = "" ]; then
echo "nothing in param"
else
echo "sending mail"

#emailing attachment
#uuencode statsNbaAllStarSim statssim | mail -s "test attachement" atayebali@foxsports.com
#uuencode <filename that will be send> <name for attached file that is sent> |

#passing the param to function
email $1
fi

Send Email in Python with Text File Attachment

// description of your code here
Send an email in python with a text attachment. You can change the attachment type by adjusting the part.startbody label in the attachment section (ie, 'image/jpg' or whatever)

import smtplib, sys, MimeWriter, StringIO, base64
import os
def mail(serverURL=None, sender='', to='', subject='', text=''):
    """
    Usage:
    mail('somemailserver.com', 'me@example.com', 'someone@example.com', 'test', 'This is a test')
    """
    message = StringIO.StringIO()
    writer = MimeWriter.MimeWriter(message)
    writer.addheader('Subject', subject)
    writer.startmultipartbody('mixed')

    # start off with a text/plain part
    part = writer.nextpart()
    body = part.startbody('text/plain')
    body.write(text)

    # now add an attachment
    part = writer.nextpart()
    part.addheader('Content-Transfer-Encoding', 'base64')
    body = part.startbody('text/plain')
    base64.encode(open('myfile.txt', 'rb'), body)

    # finish off
    writer.lastpart()

    # send the mail
    smtp = smtplib.SMTP(serverURL)
    smtp.sendmail(sender, to, message.getvalue())
    smtp.quit()

Returning A File As An Attachment From JSP

// This is truly a snippet. It's a small piece of code you use
// within a JSP file to make sure that the file you are
// returning is not interpreted by the browser. You use this
// when you want to return a TXT file, JPG or other file that
// the browser might open on its own rather than offer a
// dialog to the user so it can be saved.
//
// Note: In this example I'm returning a text file so I set
// the MIME type of the response to text/plain, but you need
// to make sure it matches the type of data you are returning.

response.setContentType("text/plain");

response.setHeader("Content-Disposition", "attachment; filename=\"test.txt\"");

Send email with attachment(s) in Python

Can't remember if I wrote this or found it on the Web or a combination, so I won't take credit per se -- I'm just posting it as reference.

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

def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
  assert type(send_to)==list
  assert type(files)==list

  msg = MIMEMultipart()
  msg['From'] = send_from
  msg['To'] = COMMASPACE.join(send_to)
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = subject

  msg.attach( MIMEText(text) )

  for f 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(f))
    msg.attach(part)

  smtp = smtplib.SMTP(server)
  smtp.sendmail(send_from, send_to, msg.as_string())
  smtp.close()
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS