Created
March 30, 2025 07:30
-
-
Save meysam81/0efddb49f2584c92b20428d205106bec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
# Function to display help information | |
maileroo_help() { | |
echo "Maileroo Email Sender" | |
echo "=====================" | |
echo "A bash function to send emails via the Maileroo API" | |
echo | |
echo "Usage: send_maileroo_email -k API_KEY -s \"Subject\" -t \"[email protected]\" -f \"Body File\" [-e \"[email protected]\"]" | |
echo | |
echo "Required Parameters:" | |
echo " -k API_KEY Your Maileroo API key" | |
echo " -s \"Subject\" Email subject line" | |
echo " -t \"[email protected]\" Recipient email address(es), comma-separated for multiple" | |
echo " -f \"file_path\" Path to file containing email body content" | |
echo | |
echo "Optional Parameters:" | |
echo " -e \"[email protected]\" Sender email address (defaults to [email protected])" | |
echo " -h, --help Display this help message" | |
echo | |
echo "Examples:" | |
echo " send_maileroo_email -k \"your-api-key\" -s \"Hello\" -t \"[email protected]\" -f \"email.html\"" | |
echo " send_maileroo_email -k \"your-api-key\" -s \"Team Update\" -t \"[email protected]\" -f \"message.txt\" -e \"[email protected]\"" | |
} | |
# Function to send emails using Maileroo API | |
send_maileroo_email() { | |
# Default values | |
local API_KEY="${MAILEROO_API_KEY}" | |
local SUBJECT="" | |
local TO="" | |
local FROM="" | |
local BODY_FILE="" | |
# Check for help flag first | |
if [[ "$1" == "--help" || "$1" == "-h" ]]; then | |
maileroo_help | |
return 0 | |
fi | |
# Parse arguments | |
while getopts ":k:s:t:f:e:h" opt; do | |
case $opt in | |
k) API_KEY="$OPTARG" ;; # API key | |
s) SUBJECT="$OPTARG" ;; # Subject | |
t) TO="$OPTARG" ;; # To (recipient) | |
f) BODY_FILE="$OPTARG" ;; # File path for body content | |
e) FROM="$OPTARG" ;; # From (sender) - optional | |
h) | |
maileroo_help | |
return 0 | |
;; # Help option | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
return 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
return 1 | |
;; | |
esac | |
done | |
# Check if required parameters are provided | |
if [[ -z "$API_KEY" || -z "$SUBJECT" || -z "$TO" || -z "$BODY_FILE" ]]; then | |
echo "Error: Missing required parameters." | |
echo "Usage: send_maileroo_email -k API_KEY -s \"Subject\" -t \"[email protected]\" -f \"Body File\" [-e \"[email protected]\"]" | |
return 1 | |
fi | |
# Check if body file exists | |
if [[ ! -f "$BODY_FILE" ]]; then | |
echo "Error: Body file not found: $BODY_FILE" | |
return 1 | |
fi | |
# Determine if the file is HTML or plain text based on extension | |
local CONTENT_TYPE="plain" | |
if [[ "$BODY_FILE" == *.html || "$BODY_FILE" == *.htm ]]; then | |
CONTENT_TYPE="html" | |
fi | |
# Read file content | |
local BODY_CONTENT=$(cat "$BODY_FILE") | |
# Call the Maileroo API | |
echo "Sending email via Maileroo API..." | |
response=$(curl -s -X POST "https://smtp.maileroo.com/send" \ | |
-H "X-API-Key: $API_KEY" \ | |
-F "from=$FROM" \ | |
-F "to=$TO" \ | |
-F "subject=$SUBJECT" \ | |
-F "${CONTENT_TYPE}=$BODY_CONTENT") | |
# Parse and display the response | |
if echo "$response" | grep -q "success\":true"; then | |
echo "Email sent successfully!" | |
else | |
echo "Failed to send email. Response:" | |
echo "$response" | |
fi | |
} | |
# Example usage: | |
# send_maileroo_email -k "your-api-key" -s "Hello from Maileroo" -t "[email protected]" -f "email_body.html" -e "[email protected]" | |
send_maileroo_email "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment