SHARE
TWEET

attmail - Send mails with attachments using sendmail

malexmave May 7th, 2012 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Author: malexmave
  3. # This code is available on GitHub at https://github.com/malexmave/attmail
  4. #
  5. # Parameter:
  6. #       attmail [From] [To] [Subject] [Body] [Attachment1] [Attachment2] ... [AttachmentX]
  7. #
  8. # Depends on the following applications to be installed and working:
  9. #       - sendmail
  10. #       - file (should be preinstalled)
  11. #
  12. # I am aware of the fact that sendmail isn't ideal, but it was the only application available on the webserver I wrote this code for.
  13. # Loosely based on Code from the following Site:
  14. # http://www.unix.com/aix/177948-how-send-attachment-using-sendmail-command-without-uuencode-command.html (Sendmail-Code)
  15. #
  16. # Notice about commented Code:
  17. # For your convenience, I left some snippets of debug code in this version. If you encounter any problems, feel free to try to debug using the proposed "echo" commands, and any additional means you see fit to use ;-)
  18. #
  19. # License:
  20. # Use this code as you see fit. I can not be held liable for any damaged to anything, including clawed out eyes when reading my code.
  21. # If you use this code, feel free to leave a note at blog.velcommuta.de/2012/coding-project-of-the-day-attmail/
  22. # You are allowed to do anything with this code, including commercial use and editing it to lessen the burden on your eyes. Please leave a note about the original author, though.
  23. #
  24. IFS=\"
  25. declare -a params=( $* )
  26. # echo "Begin PARAMS"
  27. # for (( i=0; i<${#params[*]}; i++))
  28. # do
  29. #     echo "$i=${params[i]}"
  30. # done
  31. # echo "END PARAMS"
  32. # echo
  33. declare -a mime=()
  34. for (( i=4; i<${#params[*]}; i++))
  35. do
  36.     mime[i-4]=`file --mime-encoding ${params[i]} | sed -e 's/.*: //g'`
  37. done
  38. # echo "Begin MIME"
  39. # for (( i=0; i<${#mime[*]}; i++))
  40. # do
  41. #     echo "$i=${mime[i]}"
  42. # done
  43. # echo "END MIME"
  44. # echo
  45. # echo "Begin MAIL"
  46. ( echo "to: ${params[1]}"
  47.   echo "from: ${params[0]}"
  48.   echo "subject: ${params[2]}"
  49.   echo "mime-version: 1.0"
  50.   echo "content-type: multipart/related; boundary=xxxRANDOMSTRINGxxx"
  51.   echo
  52.   echo "--xxxRANDOMSTRINGxxx"
  53.   echo "content-type: text/plain"
  54.   echo
  55.   echo "${params[3]}"
  56.   echo # Begin Loop for adding attachments
  57.   for (( i=0; i<${#mime[*]}; i++))
  58.   do
  59.       echo "--xxxRANDOMSTRINGxxx"
  60.       echo "content-type: ${mime[i]}; name=${params[i+4]}"
  61.       echo "content-transfer-encoding: base64"
  62.       echo
  63.       base64 ${params[i+4]}
  64.       echo
  65.   done
  66. ) | sendmail -t -i # comment out the pipe to get the input sendmail gets, for debugging
  67. echo "Mail sent with ${#mime[*]} attachments"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top