3.19.31.73 |    

Navigation

Google Advertisement

A little library to send email with /usr/sbin/sendmail.

sendmail.inc.sh
  1. #!/bin/bash
  2. # *****************************************************
  3. # file: sendmail.inc.sh
  4. # date: 2005-09-21 22:48
  5. # author: Marko Schulz - <info@tuxnet24.de>
  6. # *****************************************************
  7. #
  8. #
  9. # SYNOPSIS
  10. #
  11. # # load this file in your main script:
  12. # test -f ./send_mail.lib && . ./send_mail.lib || exit 1
  13. #
  14. # # define variables for mail header...
  15. # tname="Marko Schulz"            # recipient name
  16. # tmail="info@tuxnet24.de"        # recipient mail
  17. # fname="Max Mustermann"          # from name
  18. # fmail="mmustermann@example.com" # from mail
  19. # format="multipart/alternative"  # mail format: (multipart/alternative|text/plain)
  20. # subject="Testmail..."           # subject
  21. # priority="High"                 # priority: (High|Normal|Low)
  22. #
  23. #
  24. # # define your mail body...
  25. # body=$(
  26. # 
  27. # cat <<HERE
  28. # Hallo Marko,
  29. # 
  30. # das ist ein
  31. # 	Text
  32. # der so ausgegeben wird
  33. # wie er
  34. #  definiert ist.
  35. #
  36. #
  37. # HERE
  38. #
  39. # )
  40. #
  41. # # sent mail...
  42. # send_mail \
  43. # 	"$tname" \
  44. # 	"$tmail" \
  45. # 	"$fname" \
  46. # 	"$fmail" \
  47. # 	"$format" \
  48. # 	"$subject" \
  49. # 	"$priority" \
  50. # 	"$body"
  51. #
  52. #
  53. # *****************************************************
  54. # This function send a mail with the program "sendmail"
  55. # in two formats (HTML/TEXT).
  56.  
  57. function send_mail() {
  58.  
  59. local boundary=""
  60. local tname=$1; shift
  61. local tmail=$1; shift
  62. local fname=$1; shift
  63. local fmail=$1; shift
  64. local format=$1; shift
  65. local subject=$1; shift
  66. local priority=$1; shift
  67. local body="$*"
  68. local version="send_mail.lib v1.01"
  69. local mail=""
  70.  
  71. # get multiple random number or random string...
  72. if which openssl >/dev/null 2>&1; then
  73. 	boundary=$( openssl rand -base64 24 )
  74. else
  75. 	boundary=$( echo ${RANDOM}${RANDOM}${RANDOM}${RANDOM} )
  76. fi
  77.  
  78. # mail header To/From/Subject etc...
  79. mail="${mail}To: \"$tname\" <$tmail>\n"
  80. mail="${mail}Reply-to: \"$fname\" <$fmail>\n"
  81. mail="${mail}From: \"$fname\" <$fmail>\n"
  82. mail="${mail}X-Mailer: Custom-CGI: $version\n"
  83. mail="${mail}MIME-Version: 1.0\n"
  84. mail="${mail}Subject: $subject\n"
  85. mail="${mail}Importance: $priority\n"
  86.  
  87. # show mail body (HTML)...
  88. if [ "$format" = "multipart/alternative" ]; then
  89.  
  90. 	# mail format and decoding...
  91. 	mail="${mail}Content-Type: $format; boundary=\"------------$boundary\"\n"
  92. 	mail="${mail}\n"
  93. 	mail="${mail}This is a multi-part message in MIME format.\n"
  94.  
  95. 	# format TEXT...
  96. 	mail="${mail}--------------$boundary\n"
  97. 	mail="${mail}Content-Type: text/plain; charset=iso-8859-1\n"
  98. 	mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
  99. 	mail="${mail}${body}\n\n"
  100.  
  101. 	# replace newline to <br>...
  102. 	body=$( echo -e "${body}"  | \
  103. 		sed 's/\t/\ \ \ \ /g' | \
  104. 		sed 's/^ /\ /g' | \
  105. 		sed 's/$/<br \/>/g' )
  106.  
  107. 	# format HTML...
  108. 	mail="${mail}--------------$boundary\n"
  109. 	mail="${mail}Content-Type: text/html; charset=iso-8859-1\n"
  110. 	mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
  111. 	mail="${mail}<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n\n"
  112. 	mail="${mail}<html>\n"
  113. 	mail="${mail}<head>\n"
  114. 	mail="${mail}<META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">\n"
  115. 	mail="${mail}\t<title>$subject</title>\n"
  116. 	mail="${mail}<style type=\"text/css\">\n<!--\n\n"
  117. 	mail="${mail}.text{ font-family:verdana, helvetica, sans-serif; font-size:10pt; color:black;}\n"
  118. 	mail="${mail}.copyright{ font-family:verdana, helvetica, sans-serif; font-size:9pt; font-style:italic; color:black;}\n\n"
  119. 	mail="${mail}//-->\n</style>\n"
  120. 	mail="${mail}</head>\n\n"
  121. 	mail="${mail}<body bgcolor=\"#ffffff\" link=\"blue\" alink=\"blue\" vlink=\"blue\">\n"
  122. 	mail="${mail}<table border=\"0\" cellspacing=\"10\" cellpadding=\"3\" width=\"100%\" height=\"100%\">\n"
  123. 	mail="${mail}\t<tr>\n"
  124. 	mail="${mail}\t\t<td width=\"1%\"> </td>\n"
  125. 	mail="${mail}\t\t<td width=\"5%\" bgcolor=\"#f5f5f5\"> </td>\n"
  126. 	mail="${mail}\t\t<td width=\"94%\" class=\"text\" valign=\"top\">${body}"
  127. 	mail="${mail}<hr /><div align=\"right\"><span class=\"copyright\">$version"
  128. 	mail="${mail} powered by <a href=\"http://www.perlxpress.de\" target=\"_blank\">perlXpress.de</a></span></div></td>\n"
  129. 	mail="${mail}\t</tr>\n"
  130. 	mail="${mail}</table>\n"
  131. 	mail="${mail}</body>\n"
  132. 	mail="${mail}</html>\n"
  133.  
  134. 	mail="${mail}\n--------------$boundary--\n"
  135. 	mail="${mail}\n"
  136. 	# end of mail body...
  137.  
  138. # show mail body (TEXT)...
  139. else
  140.  
  141. 	# mail format and decoding...
  142. 	mail="${mail}Content-Type: $format; charset=iso-8859-1\n"
  143. 	mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
  144.  
  145. 	mail="${mail}${body}\n\n"
  146. 	mail="${mail}---------------------------------------------------------------------------\n"
  147. 	mail="${mail}\t\t$version powered by perlXpress.de\n"
  148. 	mail="${mail}---------------------------------------------------------------------------\n\n\n"
  149. 	# end of mail body...
  150.  
  151. fi
  152.  
  153. # sent mail...
  154. echo -e "${mail}" | /usr/sbin/sendmail -t
  155.  
  156. }
  157.  
  158. # *****************************************************
  159. # end of this library...
Parsed in 0.006 seconds at 870.71 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 3 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)