Mail command

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 2
Reputation: Mayank Mathur is an unknown quantity at this point 
Solved Threads: 0
Mayank Mathur Mayank Mathur is offline Offline
Newbie Poster

Mail command

 
0
  #1
Sep 7th, 2006
what command to use if i want to use both "from:" & "To:" field
in same e-mail in Linux

mail() supports subject , not "from"
Sendmail() supports "from", not "subject"

regards
Mayank
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,426
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 258
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Mail command

 
0
  #2
Sep 7th, 2006
sendmail does to include subject. You simply need to have a "Subject: ldsfgfdg" line in the text.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 2
Reputation: Mayank Mathur is an unknown quantity at this point 
Solved Threads: 0
Mayank Mathur Mayank Mathur is offline Offline
Newbie Poster

Re: Mail command

 
0
  #3
Sep 7th, 2006
it's not working
i m using sendmail() command in Linux shell script
let me know if u can give me complete syntax of sendmail command with "sub:" field included
regards,
Mayank
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Mail command

 
0
  #4
Sep 7th, 2006
Use
Shell Scripting Syntax (Toggle Plain Text)
  1. mailx -r '<from name>' -s '<subject here>' john@somewhere.com < message_text_file
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,426
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 258
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Mail command

 
0
  #5
Sep 8th, 2006
Maybe this is just self-promotion, and then again, maybe it will help you. Here is a perl script I have written for sending mail with attachments as a command line utility for unix. It uses the GetOpt:TD, Net:omain, and MIME::Base64 Modules, all of which should be in the standard Perl distribution. It sends a multipart mime message using sendmail (with a subject). It sets the From, To, CC, and BCC Addressees as well. You will almost definately need to change the top line to point to your perl command.

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl -w
  2. #########################################################################
  3. # #
  4. # Name: maila #
  5. # #
  6. # Author: Martin L. Simons #
  7. # #
  8. # Usage: maila [-a <attach,...>] [-b <bcc,...>] [-c <cc,...>] #
  9. # [-f <from>] [-h] [-t <text file>] [-T <Text string>] #
  10. # [-r] [-s <subj>] [-p <path>] <Addressee> [<Addressee>] #
  11. # Options: #
  12. # -a comma seperated list of files to send as attachments. #
  13. # -b comma seperated list of blind carbon copy recipients. #
  14. # -c comma seperated list of carbon copy recipients. #
  15. # -f full address for the "From:" line (Default: User@Host.Domain). #
  16. # -h print this message. #
  17. # -p Full path to sendmail command (Default: /usr/lib/sendmail). #
  18. # -r set the official replier. #
  19. # -s subject (Default: Mail from "From Addressee"). #
  20. # -t name of file containing text for mail (without this or the -T #
  21. # option the message will be typed until a "." by itself). #
  22. # -T a text string to include as the body of the message (see -t). #
  23. # Arguments: #
  24. # <Addressee> The "To:" Addressees. #
  25. # #
  26. # Pupose: To eable the mailing of attachments from a unix machine. #
  27. # #
  28. # Exit Codes: #
  29. # #
  30. # 0 -- Everything was successful #
  31. # 1 -- Invalid Options #
  32. # 2 -- Both option -t and -T were provided #
  33. # 3 -- No "To:" addressees provided #
  34. # 4 -- sendmail not found or not executable #
  35. # 5 -- sendmail error. #
  36. # #
  37. #########################################################################
  38.  
  39. #########################################################################
  40. # #
  41. # BEGIN subroutine #
  42. # Arguments: None #
  43. # Purpose: Set the script name and directory variables immediately #
  44. # #
  45. #########################################################################
  46.  
  47. sub BEGIN {
  48. use File::Basename;
  49. $main::me = basename($0);
  50. $main::libdir = dirname($0);
  51. }
  52.  
  53. #
  54. # use strict and lib and declare global variables.
  55. #
  56. use strict;
  57. use lib "$main::libdir";
  58. use vars qw($opt_a $opt_b $opt_c $opt_f $opt_h
  59. $opt_p $opt_r $opt_s $opt_t $opt_T);
  60.  
  61. #
  62. # include needed modules
  63. #
  64. use Getopt::Std;
  65. use MIME::Base64;
  66. use Net::Domain qw(hostname hostfqdn hostdomain);
  67. require Sys::Hostname;
  68.  
  69. #
  70. # set the path
  71. #
  72. $ENV{PATH} = $main::libdir . ":" . $ENV{PATH} .
  73. ":/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin";
  74.  
  75. #########################################################################
  76. # #
  77. # usage subroutine #
  78. # Arguments: Exit Code value #
  79. # Purpose: Print the usage message and exit with given exit code. #
  80. # #
  81. #########################################################################
  82.  
  83. sub usage ($) {
  84. my $rc = shift;
  85.  
  86. my $sp = " " x length($main::me);
  87. print STDERR <<EOF;
  88.  
  89. Usage: $main::me [-a <attach,attach,...>] [-b <bcc,bcc,...>] [-c <cc,cc,...>]
  90. $sp [-f <from>] [-h] [-t <text file>] [-T <Text string>]
  91. $sp [-r] [-s <subject>] [-p <sendmail path>] <Addressee> [<Addressee>]
  92.  
  93. Options:
  94. -a comma seperated list of files to send as attachments.
  95. -b comma seperated list of blind carbon copy recipients.
  96. -c comma seperated list of carbon copy recipients.
  97. -f full address to appear on "From:" line (Default: User\@Host.Domain).
  98. -h print this message.
  99. -p Full path to sendmail command (Default: /usr/lib/sendmail).
  100. -r set the official replier (must be previleged sendmail user or root).
  101. -s subject (Default: Mail from "From Addressee").
  102. -t name of file containing body text for mail (without this or the -T
  103. option the body of the message will be typed until a "." by itself).
  104. -T a text string to include as the body of the message (see -t).
  105.  
  106. Arguments:
  107. <Addressee> The "To:" Addressees.
  108.  
  109. EOF
  110.  
  111. exit($rc);
  112. }
  113.  
  114. #
  115. # Process Options
  116. #
  117. &usage(1) unless (getopts('a:b:c:f:hp:rs:t:T:'));
  118.  
  119. #
  120. # check for -t -T
  121. #
  122. if ((defined($opt_t)) && (defined($opt_T))) {
  123. print STDERR "Options <t> and <T> are mutually exclusive.\n";
  124. exit(2);
  125. }
  126.  
  127. #
  128. # -h
  129. #
  130. &usage(0) if (defined($opt_h));
  131.  
  132. #
  133. # check for "To:" Addressees
  134. #
  135. &usage(3) unless (scalar(@ARGV) > 0);
  136.  
  137. #
  138. # Check sendmail
  139. #
  140. my $sendmail = ((defined($opt_p)) ? $opt_p : "/usr/lib/sendmail");
  141. unless (-x $sendmail) {
  142. print STDERR "Cannot find $sendmail.";
  143. print STDERR " Try defining it with -p.\n" unless (defined($opt_p));
  144. print STDERR " Check the entered path again.\n" if (defined($opt_p));
  145. exit(4);
  146. }
  147.  
  148. #
  149. # create a search path from CDPATH,cdpath,PATH
  150. #
  151. my @dirs = ();
  152. push(@dirs, split(/:/, $ENV{CDPATH})) if (defined($ENV{CDPATH}));
  153. push(@dirs, split(/:/, $ENV{cdpath})) if (defined($ENV{cdpath}));
  154. push(@dirs, split(/:/, $ENV{PATH}));
  155.  
  156. #
  157. # Search for the provided file.
  158. #
  159. my @files = ();
  160. if (defined($opt_a)) {
  161. foreach my $file (split(/,/, $opt_a)) {
  162. if (-f $file) {
  163. push(@files, $file);
  164. } else {
  165. my $temp = $file;
  166. foreach my $dir (@dirs) {
  167. if ((defined($dir)) && (-f $dir . "/" . $file)) {
  168. push(@files, $dir . "/" . $file);
  169. $temp = $dir . "/" . $file;
  170. last;
  171. }
  172. }
  173. print STDERR "WARNING: Cannot fine $file, skipping.\n"
  174. if ($file eq $temp);
  175. }
  176. }
  177. }
  178.  
  179. #
  180. # Set the carbon copy lists.
  181. #
  182. my @bcc = split(/,/, $opt_b) if (defined($opt_b));
  183. my @cc = split(/,/, $opt_c) if (defined($opt_c));
  184.  
  185. #
  186. # Determine the from addressee
  187. #
  188. my $from = ((defined($opt_f)) ? $opt_f : undef);
  189. unless (defined($from)) {
  190. my $host = hostfqdn();
  191. unless (defined($host)) {
  192. $host = hostname();
  193. my $domain = `domainname`;
  194. if (defined($domain)) {
  195. chomp($domain);
  196. $host .= (($domain !~ m/^\s*$/) ? "." . $domain : "");
  197. }
  198. }
  199. my $user = (getpwuid($<))[0];
  200. unless (defined($user)) {
  201. if (defined($ENV{USER})) {
  202. $user = $ENV{USER};
  203. } else {
  204. $user = `who am i`;
  205. chomp($user);
  206. $user = (split(/\s+/, $user))[0];
  207. }
  208. }
  209. $from = $user . '@' . $host;
  210. }
  211.  
  212. #
  213. # Determine sendmail options and the subject.
  214. #
  215. my $opts = ((defined($opt_r)) ? qq[-t -r "$from"] : "-t ");
  216. my $subj = ((defined($opt_s)) ? $opt_s : qq[Mail from $from]);
  217.  
  218. #
  219. # Print a warning if the mail will be empty of text.
  220. #
  221. if (defined($opt_t)) {
  222. if ((! -f $opt_t) || (-z $opt_t)) {
  223. print STDERR "WARNING: $opt_t is empty or does not exist.";
  224. print STDERR " Mail will contain no text.\n";
  225. $opt_t = "User Entered Empty File";
  226. }
  227. }
  228.  
  229. #
  230. # Execute sendmail
  231. #
  232. unless (open(MAIL, "| $sendmail $opts")) {
  233. print STDERR "Could not execute $sendmail: $!\n";
  234. exit(5);
  235. }
  236.  
  237. #
  238. # Print the addresses and subject
  239. #
  240. print MAIL "From: $from\n";
  241. print MAIL "To: " . join(", ", @ARGV) . "\n";
  242. print MAIL "Cc: " . join(", ", @cc) . "\n";
  243. print MAIL "Bcc: " . join(", ", @bcc) . "\n";
  244. print MAIL "Subject: $subj\n";
  245.  
  246. #
  247. # Make it a multpart message if needed
  248. #
  249. if (scalar(@files) > 0) {
  250. print MAIL "Mime-Version: 1.0\n";
  251. print MAIL "Content-Type: multipart/mixed; BOUNDARY=A_Mail_SCR_0\n\n";
  252. print MAIL "--A_Mail_SCR_0\n";
  253. print MAIL "Content-Type: text/plain; charset=us-ascii\n\n";
  254. } else {
  255. print MAIL "Content-Type: text/plain; charset=us-ascii\n\n";
  256. }
  257.  
  258. #
  259. # retreive mail text either "string", text file, or STDIN
  260. #
  261. if (defined($opt_T)) {
  262. print MAIL "$opt_T\n\n\n\n\n\n\n";
  263. } elsif (defined($opt_t)) {
  264. unless ($opt_t eq "User Entered Empty File") {
  265. if (open(FILE, "<$opt_t")) {
  266. my $orig = $/;
  267. $/ = undef;
  268. print MAIL <FILE>;
  269. close(FILE);
  270. $/ = $orig;
  271. print MAIL "\n\n\n\n\n\n";
  272. } else {
  273. print STDERR "WARNING: Could not open $opt_t.";
  274. print STDERR " Mail will contain no text\n";
  275. }
  276. }
  277. } else {
  278. print STDOUT "\nNow enter your text body ending with a line containing\n";
  279. print STDOUT qq[either a ".", "EOF", or "EOT" on a line by itself.\n\n];
  280. while(<STDIN>) {
  281. chomp;
  282. if (m/^(\.|EOF|EOT)$/) {
  283. last;
  284. }
  285. print MAIL $_, "\n";
  286. }
  287. print MAIL "\n\n\n\n\n\n";
  288. }
  289.  
  290. #
  291. # Add each attachment. Read file in 57 byte blocks to ensure 76 char
  292. # base64 encode lines.
  293. #
  294. foreach my $file (@files) {
  295. unless (open(FILE, $file)) {
  296. print STDERR "Could not open $file: $!\n";
  297. next;
  298. }
  299. my $temp = `file $file`;
  300. chomp $temp;
  301. $temp =~ s/^\s*[^:]*:\s+//;
  302. print MAIL "\n--A_Mail_SCR_0\n";
  303. print MAIL "Content-Type: APPLICATION/octet-stream; name=", basename($file);
  304. print MAIL "\nContent-Transfer-Encoding: BASE64\n";
  305. print MAIL "Content-Description: ", basename($file), " (", $temp, ")\n\n";
  306. my $read = "\0" x 57;
  307. while (read(FILE, $read, 60*57)) {
  308. print MAIL encode_base64($read);
  309. }
  310. close(FILE);
  311. }
  312.  
  313. #
  314. # Inform user and exit.
  315. #
  316. print STDOUT "\nMail Sent.\n";
  317. exit(0);
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 30
Reputation: sut is an unknown quantity at this point 
Solved Threads: 1
sut sut is offline Offline
Light Poster

Re: Mail command

 
0
  #6
Sep 11th, 2006
Set the variables as needed first, then:

Shell Scripting Syntax (Toggle Plain Text)
  1. (
  2. cat <<!
  3. From: $FROM
  4. Subject: $SUBJ
  5. To: $TO
  6. !
  7.  
  8. [ "$CC" ] && echo "Cc: $CC"
  9.  
  10. echo
  11.  
  12. cat $BODY ) | sendmail
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC