mailit 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #!/usr/bin/perl
  2. #
  3. # mailit
  4. #
  5. # Copyright (c) 1998, 2016 by Peter_Siegrist(SystemLoesungen) (PSS@ZweierNet.ch)
  6. #
  7. # All Rights reserved.
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License as
  10. # published by the Free Software Foundation.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. #----------------------------------------------
  18. #-- INITIAL SETTINGS TO BE MODIFIED BY USERS
  19. #----------------------------------------------
  20. BEGIN {
  21. #-- Set Path to MIME::Lite Module if not in @INC
  22. #-- i.e. $MIME_LITE_PATH = "/home/myhome/lib/";
  23. #-- uncomment both of the next two lines therefor
  24. # $MIME_LITE_PATH = "$ENV{HOME}/lib/"; # <=== here we are
  25. # unshift( @INC, "$MIME_LITE_PATH" )
  26. }
  27. #-- Set Host / Domain Part of email Address: name@$DOMAIN_NAME
  28. #-- i.e. $DOMAIN_NAME = "myhost.ch";
  29. $DOMAIN_NAME = "yourdomail.tld"; # <=== here we are
  30. #-- END INITIAL SETTINGS
  31. #----------------------------------------------
  32. #-- Modules
  33. #----------------------------------------------
  34. use MIME::Lite;
  35. use Getopt::Std;
  36. #----------------------------------------------
  37. #-- VAR's
  38. #----------------------------------------------
  39. $body = "";
  40. my %mimes = ( '.doc' => 'application/msword',
  41. '.rtf' => 'application/rtf',
  42. '.xls' => 'application/excel',
  43. '.xlt' => 'application/excel',
  44. '.pps' => 'application/pps',
  45. '.ppt' => 'application/ppt',
  46. '.pot' => 'application/pot',
  47. '.fmx' => 'application/x-framemaker',
  48. '.ai ' => 'application/postscript',
  49. '.eps' => 'application/postscript',
  50. '.ps ' => 'application/postscript',
  51. '.sxw' => 'application/msword',
  52. '.odt' => 'application/vnd.oasis.opendocument.text',
  53. '.ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  54. '.odp' => 'application/vnd.oasis.opendocument.presentation',
  55. '.odg' => 'application/vnd.oasis.opendocument.graphics',
  56. '.odc' => 'application/vnd.oasis.opendocument.chart',
  57. '.odb' => 'application/vnd.oasis.opendocument.database',
  58. '.odm' => 'application/vnd.oasis.opendocument.text-master',
  59. '.odf' => 'application/vnd.oasis.opendocument.formula',
  60. '.html' => 'text/html',
  61. '.htm' => 'text/html',
  62. '.xml' => 'text/xml',
  63. '.xsl' => 'text/xml',
  64. '.jpg' => 'image/jpeg',
  65. '.jpeg' => 'image/jpeg',
  66. '.jpe' => 'image/jpeg',
  67. '.png' => 'image/png',
  68. '.gif' => 'image/gif',
  69. '.bmp' => 'image/bmp',
  70. '.tiff' => 'image/tif',
  71. '.tif' => 'image/tif',
  72. '.xbm' => 'image/x-xbitmap',
  73. '.zip' => 'application/zip',
  74. '.Z' => 'application/x-tar',
  75. '.gz' => 'application/x-tar',
  76. '.tar' => 'application/x-tar',
  77. '.pcap' => 'application/pcap',
  78. '.hqx' => 'application/mac-binhex40',
  79. '.pdf' => 'application/pdf'
  80. );
  81. #----------------------------------------------
  82. #-- SUB's
  83. #----------------------------------------------
  84. sub usage {
  85. print "usage: $0 \n\t-f [<from>] \n\t-t <to>\t\t\tList possible\n\t-c [<cc>]\t\tList possible\n\t-s [<subject>] \n\t-m [<mime-type:filename>] \teg. application/pdf:doc.pdf\n\t-a [<attachment>]\tComma separated list of files to attach (no spaces)\n\t-p\t\t\tread body data from STDIN if set\n\t-h help\n\t-H Manual Page\n";
  86. }
  87. #----------------------------------------------
  88. #-- MAIN
  89. #----------------------------------------------
  90. #----------------------------------------------
  91. #-- get options:
  92. #----------------------------------------------
  93. # -f <from>
  94. # -t <to>
  95. # -c [<cc>] comma separated
  96. # -s <subject>
  97. # -a <attachment(s)> comma separated
  98. # -m <mime-type:filename> eg. application/pdf:attachmentpdf
  99. # -b <body text if -m is used>
  100. # -p read from pipe if set
  101. # -h help
  102. # -H manpage
  103. getopt('tscafmb');
  104. my $p = 0;
  105. my $from = "$ENV{USER}@$DOMAIN_NAME";
  106. my $subject = "No Subject";
  107. my $to = undef;
  108. my $cc = "";
  109. my $ifile = "";
  110. my $imime = "";
  111. my @attachment = ();
  112. $opt_h && do { &usage;
  113. exit;
  114. };
  115. $opt_H && do { while ( <DATA> ) { print; }
  116. exit;
  117. };
  118. $opt_f && do { $from = $opt_f;
  119. };
  120. $opt_s && do { $subject = $opt_s;
  121. };
  122. $opt_c && do { $cc = $opt_c;
  123. };
  124. $opt_m && do { ($imime, $ifile) = split ':', $opt_m;
  125. };
  126. $opt_b && do { $itext = $opt_b;
  127. };
  128. $opt_a && do { @attachment = split ',', $opt_a;
  129. };
  130. $opt_p && do { $p = 1;
  131. };
  132. if ( $opt_t ) { $to = $opt_t;
  133. } else {
  134. print "missing mandatory argument: -t\n";
  135. &usage;
  136. exit;
  137. }
  138. #----------------------------------------------
  139. #-- read body from stdin ( if option p is set )
  140. #----------------------------------------------
  141. if ( $p == 1 ) {
  142. while ( <STDIN> ) {
  143. last if /^\.$/; # Finish input with single dot line
  144. $body .= $_;
  145. }
  146. }
  147. #----------------------------------------------
  148. #-- create mime object
  149. #----------------------------------------------
  150. chomp($to);
  151. chomp($from);
  152. $m = MIME::Lite->new(
  153. From => $from,
  154. To => $to,
  155. Subject => $subject,
  156. Type => 'multipart/mixed');
  157. if ( $opt_m ) {
  158. attach $m Type => 'TEXT',
  159. Encoding => 'quoted-printable',
  160. Data => "$itext";
  161. attach $m
  162. Type => $imime,
  163. Encoding => 'quoted-printable',
  164. Disposition => 'attachment',
  165. Data => $body,
  166. Filename => "$ifile";
  167. } else {
  168. attach $m Type => 'TEXT',
  169. Encoding => 'quoted-printable',
  170. Data => $body;
  171. }
  172. if ( $cc ) {
  173. $m->add( Cc => $cc );
  174. }
  175. #----------------------------------------------
  176. #-- parse filetyp / attach files
  177. #----------------------------------------------
  178. while ( defined( $att = shift @attachment ) ) {
  179. my ($ftyp) = ($att =~ /.+(\.\w{1,4})$/i);
  180. $ftyp = lc $ftyp;
  181. if ( exists $mimes{$ftyp} ) {
  182. if ( $mimes{$ftyp} =~ /text\// ) {
  183. attach $m
  184. Type => $mimes{$ftyp},
  185. Encoding => 'quoted-printable',
  186. Disposition => 'attachment',
  187. Path => "$att";
  188. next;
  189. } else {
  190. attach $m
  191. Type => $mimes{$ftyp},
  192. Disposition => 'attachment',
  193. Path => "$att";
  194. next;
  195. }
  196. } else {
  197. attach $m
  198. Type => 'text/plain',
  199. Encoding => 'base64',
  200. Disposition => 'attachment',
  201. Path => "$att";
  202. next;
  203. }
  204. }
  205. #----------------------------------------------
  206. #-- send message
  207. #----------------------------------------------
  208. $m->send;
  209. __END__
  210. NAME
  211. mailit - send mail to users, including attachments
  212. SYNOPSIS
  213. mailit [ -hH ]
  214. -t recipient,... [ -f sender ] [ -c recipient,... ]
  215. [ -a attachment,... ] [ -m mime-type:filename ] [ -b body text if -m ] [ -s subject ] [ -p ]
  216. DESCRIPTION
  217. mailit is a front end to send mails including attachments from
  218. the command line. It uses sendmail to deliver the message to the
  219. corret place. mailit is based on MIME::Lite from ZeeGee Software
  220. Inc.
  221. With -p flag set it reads from standard input up to an EOF or a
  222. single dot line. That means it can read some data through a Unix
  223. Pipe till EOF or, if you prefere read in interactively, terminate
  224. input with a single dot in the line. In interactive mode mailit
  225. will prompt you for the body.
  226. The data you entered this way will be sent as the body of the mail
  227. setting text/plain as Content-Type.
  228. mailit determines the 'sender address' from both the environment
  229. variable HOME for the 'user' part as well as the source setting
  230. $DOMAIN_NAME as 'domain'.
  231. OPTIONS
  232. -t recipient,...
  233. email address(es) of the recipient(s). If more than one separate
  234. by commas. (the To: Field)
  235. -f sender
  236. sender of the email if you not want to determine it by mailit.
  237. (The From: Field)
  238. -c recipient
  239. one or more CC: addresses. (The CC: Field)
  240. -a attachment
  241. one or more comma separated file names to be attached to the
  242. mail. see below for further discussion.
  243. -m mime-type:filename
  244. the mime-type and the filename of the document. This is mainly used for
  245. situations where you pipe (-p) a document into mailit without any
  246. information about the mime-type.
  247. Mail recipients get the piped data as attachment of type 'mime-type'
  248. with name 'filename'. Use the -b parameter to give the body a text.
  249. -b body text for -m
  250. the body text if piping some data with -m and -p to the program .
  251. -s subject
  252. the subject of the mail. Quote it if you use Special Characters.
  253. (The Subject: Field)
  254. -p mailit reads the body from standard input up to an EOF or, in
  255. interactive mode up to a single dot line. Without the -p flag
  256. mailit does not read nor send any body.
  257. -h help.
  258. -H man page like help.
  259. ATTACHMENTS
  260. mailit knows some type of files so it can set the correct
  261. Content-Type as well as the correct Encoding of the attachment.
  262. Text Media Types are encoded as 'quoted-printable' whereas other
  263. known Media Types are encoded 'base64'.
  264. You can expand the list of known Media Types by adding a
  265. 'type => encoding' entry in the %mimes hash.
  266. All unknown Media Types will be sent as 'text/plain' with 'binary'-Encoding.
  267. INSTALLATION
  268. mailit uses the MIME::Lite Modul for creating Mail Headers.
  269. MIME::Lite for his part uses 'sendmail' to send the message.
  270. Therefore you have to install 'sendmail' as well as the
  271. MIME::Lite Modul from ZeeGee Software Inc. (www.zeegee.com).
  272. If this modul can not be found in the @INC Array, that means you
  273. do not have a standard installation of this module therefore you have to set
  274. the path of the module in the INITIAL USER SETTINGS Part of the
  275. source script ($MIME_LITE_PATH). (see there).
  276. In some cases mailit can not determine the correct Senders domain
  277. name. If so, set the Senders domain name in the INITIAL USER
  278. SETTINGS Part of the source script ($DOMAIN_NAME). (see there).
  279. ENVIRONMENT VARIABLES
  280. mailit takes the senders name (that is, the user part of the email
  281. address (user@...) ) from the Environment Variable USER. Make sure
  282. this variable is set correctly.
  283. EXAMPLES
  284. To send just two Attachments. According to standard the Subject
  285. in this case is set to 'No Subject'.
  286. mailit -t shorty@host.ch -a /doc/file1.html,/doc/file2.tar.Z
  287. Full use:
  288. cat body.txt | mailit -t shorty@host.ch,secnd@addr.ch
  289. -f froma@host2.ch -a /doc/file1.html,/doc/file2.tar.Z
  290. -c cc1@addr.ch,cc2@addr.ch -p -s "This my Files"
  291. -m and -b use:
  292. somefile2pdf somefile.xxx | mailit -t shorty@host.ch -m "application/pdf:somefile.pdf" -b "Attached file for you" -p
  293. NOTES
  294. mailit is a Perl Program. You will probably need Perl 5.005 or
  295. better as well as Mime-Lite 2.117 or better to run.
  296. Suggestions are welcome.
  297. AUTHOR
  298. Peter Siegrist http://PSS.ZweierNet.ch (pss@ZweierNet.ch)