[Tutor] How to attach file with mail

Abhiram Singh Kushwah Abhiram Singh Kushwah" <abhiramkushwah@rediffmail.com
13 Dec 2001 05:32:48 -0000

Hi all,=0D=0A	How to attach a file with this code?=0D=0A=0D=0A> #!/usr/loca=
l/bin/python=0D=0A>                                 # so the script is =0D=
=0A> executable=0D=0A> import smtplib                  # import the smtp =
=0D=0A> routines=0D=0A> import string                   # and string =0D=0A=
> manipulation stuff=0D=0A> =0D=0A> CRLF=3D("\r"+"\n")                     =
   # we will be =0D=0A> tagging CRLF onto=0D=0A> several things.=0D=0A>    =
                                     # so this is =0D=0A> handy.=0D=0A> =0D=
=0A> def prompt(prompt):=0D=0A>         return string.strip(raw_input(promp=
t))  # =0D=0A> prompt, get, strip,=0D=0A> return.=0D=0A> =0D=0A> =0D=0A> fr=
omaddr =3D prompt("From: ")             # get the from =0D=0A> address.=0D=
=0A> toaddrs  =3D prompt("To: ")               # get the to =0D=0A> address=
.=0D=0A> subject =3D prompt("Subject: ")           # get the =0D=0A> subjec=
t.=0D=0A> =0D=0A> print "Enter message, end with ^D:"     # Add the From: =
=0D=0A> and To: headers=0D=0A> at the start!=0D=0A> =0D=0A> msg =3D "From: =
" + fromaddr + CRLF + "To: " + toaddrs + =0D=0A> CRLF + "Subject: "=0D=0A> =
+ subject + CRLF +=0D=0A>  CRLF=0D=0A> =0D=0A> while 1:=0D=0A>         try:=
=0D=0A>                 line =3D raw_input()=0D=0A>         except EOFError=
:=0D=0A>                 break=0D=0A>         if not line:=0D=0A>          =
       break=0D=0A>         msg =3D msg + line + CRLF=0D=0A> =0D=0A> print =
type(msg)=0D=0A> =0D=0A> print "Message length is " , len(msg) , " bytes lo=
ng."=0D=0A> =0D=0A> server =3D smtplib.SMTP('localhost')=0D=0A> server.set_=
debuglevel(1)=0D=0A> server.sendmail(fromaddr, toaddrs, msg)=0D=0A> server.=
quit()=0D=0A>  =0A