mail_server_one.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/sh
  2. usage() {
  3. echo "Usage: $0 -s server -t \"title\" -c \"content\" [-r reward] [-l level] [-m money]"
  4. echo "Or (backward compatible): $0 serNum \"title\" \"content\" [reward] [level] [money]"
  5. exit 1
  6. }
  7. # Default values
  8. reward=0
  9. level=0
  10. money=0
  11. # Parse options
  12. while getopts "s:t:c:r:l:m:h" opt; do
  13. case $opt in
  14. s) serNum="$OPTARG" ;;
  15. t) title="$OPTARG" ;;
  16. c) content="$OPTARG" ;;
  17. r) reward="$OPTARG" ;;
  18. l) level="$OPTARG" ;;
  19. m) money="$OPTARG" ;;
  20. h) usage ;;
  21. *) usage ;;
  22. esac
  23. done
  24. # If no options provided, fall back to positional (backward compatibility)
  25. if [ -z "$serNum" ]; then
  26. serNum="$1"
  27. title="$2"
  28. content="$3"
  29. reward="${4:-0}"
  30. level="${5:-0}"
  31. money="${6:-0}"
  32. fi
  33. # Validate required
  34. if [ -z "$serNum" ]; then
  35. echo "server id is required"
  36. usage
  37. fi
  38. # Basic URL-encode for a few characters (space, &, ?, =, +)
  39. # This is not a full encoder but handles common problematic chars for URLs in this script.
  40. urlencode() {
  41. local s="$1"
  42. s=$(printf '%s' "$s" | sed -e 's/ /%20/g' -e 's/&/%26/g' -e 's/?/%3F/g' -e 's/=/ %3D/g' -e 's/+/ %2B/g')
  43. # Trim accidental spaces introduced above
  44. s=$(printf '%s' "$s" | sed 's/ */ /g')
  45. printf '%s' "$s"
  46. }
  47. # ...existing code... (server list lookup)
  48. url=""
  49. ro=""
  50. for server in `cat /data/shell/jenkins/serverlist`;do
  51. attr1=`echo $server | cut -d',' -f1`
  52. attr2=`echo $server | cut -d',' -f2`
  53. attr3=`echo $server | cut -d',' -f3`
  54. if [[ $attr1 == $serNum ]];then
  55. url=$attr2
  56. ro=$attr3
  57. break
  58. fi
  59. done
  60. if [[ $url == "" ]];then
  61. echo "服务器未配置,需要手动添加"
  62. exit 0
  63. fi
  64. echo "传入的服务器id:${serNum}"
  65. echo "传入的邮件标题:${title}"
  66. echo "传入的邮件内容:${content}"
  67. echo "传入的邮件奖励:${reward}"
  68. echo "传入的等级筛选:${level}"
  69. echo "传入的充值筛选:${money}"
  70. # URL-encode title and content (basic)
  71. enc_title=$(urlencode "${title}")
  72. enc_content=$(urlencode "${content}")
  73. if [[ $reward == "0" ]];then
  74. url="$url/gm/mail?type=add&id=1&title=$enc_title&content=$enc_content"
  75. else
  76. url="$url/gm/mail?type=add&id=1&title=$enc_title&content=$enc_content&reward=$reward"
  77. fi
  78. # Query uids and send mails
  79. uids=$(mysql -uroot -p123456 "$ro" -N -e "select uid from role where base_level>=${level} and total_recharge>=${money};")
  80. for id in ${uids}; do
  81. curl "${url}&uid=${id}"
  82. done
  83. echo "-----执行完毕-----"