How to set Content-Type in s-nail? - bash

I am using s-nail to send emails via SMTP by a bash script. Here is an example:
cat data.html | s-nail -v \
-s "Subject Text" \
-r "reply#abc.de" \
-S smtp=smtp.abc.de:587 \
-S smtp-use-starttls \
-S smtp-auth=login \
-S smtp-auth-user="admin#abc.de" \
-S smtp-auth-password="password1234" \
-a foo.img \
foo#bar.com
By default the content type is being set to Text.
How to set the Content-Type to html?

A little late but here you go
-M "text/html"

Related

Bash scripts return a substring not whole value in column for psql

I want to get the value of message in db, which for example is "the error is missing index " but when I run the code belong, it only return me 'the' for error_type. How could this happen that only return me the first word but not the whole value of the query result? How can I get the query results with the whole value?
declare -a ROW=($(psql \
-X \
-U $DB_USER \
-h $DB_HOST \
-d $DB_NAME \
-p $DB_PORT \
--single-transaction \
--set AUTOCOMMIT=off \
--set ON_ERROR_STOP=on \
--no-align \
-t \
--field-separator ' ' \
--quiet \
-c "SELECT message
FROM error_message
WHERE created_at > '$t1' and created_at < '$t'")
)
error_type=${ROW[0]}
echo $error_type
Don't use an array, use an ordinary string variable to contain the whole result.
error_type=$(psql \
-X \
-U $DB_USER \
-h $DB_HOST \
-d $DB_NAME \
-p $DB_PORT \
--single-transaction \
--set AUTOCOMMIT=off \
--set ON_ERROR_STOP=on \
--no-align \
-t \
--field-separator ' ' \
--quiet \
-c "SELECT message
FROM error_message
WHERE created_at > '$t1' and created_at < '$t'")
echo "$error_type"
Remember to quote variables unless you need word splitting and wildcard expansion to be done.

Submit multiple json payloads with curl

I am working with the confluent kafka, zookeeper in docker. I successfully submit a json file to kafka topic then consume as follow
curl -X POST \
-H "Content-Type: application/json" \
--data '{"name": "quickstart-file-source", "config {"connector.class":"org.apache.kafka.connect.file.FileStreamSourceConnector", "tasks.max":"1", "topic":"quickstart-data", "file": "/tmp/quickstart/input.json"}}' \
http://localhost:28081/connectors
Above curl command has only one json file which executes successfully but I need to post multiple json files. Is there any way to do it?
Here is my kafka connect
docker run -d \
--name=kafka-connect-avro \
--net=host \
-e CONNECT_BOOTSTRAP_SERVERS=localhost:29091 \
-e CONNECT_REST_PORT=28081 \
-e CONNECT_GROUP_ID="quickstart-avro" \
-e CONNECT_CONFIG_STORAGE_TOPIC="quickstart-avro-config" \
-e CONNECT_OFFSET_STORAGE_TOPIC="quickstart-avro-offsets" \
-e CONNECT_STATUS_STORAGE_TOPIC="quickstart-avro-status" \
-e CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR=1 \
-e CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR=1 \
-e CONNECT_STATUS_STORAGE_REPLICATION_FACTOR=1 \
-e CONNECT_KEY_CONVERTER="org.apache.kafka.connect.json.JsonConverter" \
-e CONNECT_VALUE_CONVERTER="org.apache.kafka.connect.json.JsonConverter" \
-e CONNECT_INTERNAL_KEY_CONVERTER="org.apache.kafka.connect.json.JsonConverter" \
-e CONNECT_INTERNAL_VALUE_CONVERTER="org.apache.kafka.connect.json.JsonConverter" \
-e CONNECT_REST_ADVERTISED_HOST_NAME="localhost" \
-e CONNECT_LOG4J_ROOT_LOGLEVEL=DEBUG \
-v /tmp/quickstart/file:/tmp/quickstart \
confluentinc/cp-kafka-connect:latest
Reference link
You could make individual JSON files in the current directory and post them separately in a loop
e.g.
$ ls *.json # list your connectors
payload1.json
payload2.json
And then loop over them
for f in `ls *.json`; do
curl -X POST -H "Content-Type: application/json" \
--data#${f} http://localhost:28081/connectors
done
Or simpler to use cat

mailgun e-mail attachment cronjob

I have a bash script with the below content, It works fine when i run it directly, If i add this to cron job it fails in attachment.
#!/bin/bash
echo "Clearing the files"
rm -f ci/assets/*.csv
sleep 10
echo "Shot report..."
node ci/export-shot.js
echo "Not shot report..."
node ci/export-not-shot.js
echo "Retouching delivered report..."
node ci/export-retouching-delivered.js
echo "Sending email"
curl -s --user 'api:key-6gdgdfg852fgggy4893g-t5cjgfseerwefgfdgdf2183' \
https://api.mailgun.net/v3/test.co/messages \
-F from='Openly Report Bot <report-bot#test.co>' \
-F to='kirthan.b#test.co' \
-F subject="`date -v-1d +%F` : Shot/Not Shot/Delivered Articles" \
-F text='Daily report of shot, not shot and delivered articles' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=#ci/assets/shot.csv \
-F attachment=#ci/assets/not-shot.csv \
-F attachment=#ci/assets/delivered.csv \
Please help me on this.

How to get a specific string from a variable using Shell Script

I have a code which returns an access token string.
#!/bin/bash
token=$(curl --globoff -X POST "https://example.com/token" -F 'grant_type=client_credentials' -F 'client_id=d86fc9b690963b8dda602bd26f33454r457e9024a4ecccf4c3bbf66ffcbc241b' -F 'client_code'='ff148c56118728b62c9f2ew3e4r009a7a1c645276b70611fa32fa055b9944934')
echo "$token" > Token.txt
The output given by this command is :
{
"access_token": "5048e7d38e73b4a809a4fcb219b63ae34f34f43f83d6663ffd777203afb5654ab",
"token_type": "bearer",
"expires_in": 7200
}
ie. the variable token contains the above result. My question is how to get the access token 5048e7d38e73b4a809a4fcb219b63ae34f34f43f83d6663ffd777203afb5654ab alone from this and save to another variable.
First try jq, if not possible, this is a workaround using awk:
access_token=$(curl --globoff -X POST "https://example.com/token" \
-F 'grant_type=client_credentials' \
-F 'client_id=d86fc9b690963b8dda602bd26f33454r457e9024a4ecccf4c3bbf66ffcbc241b' \
-F 'client_code'='ff148c56118728b62c9f2ew3e4r009a7a1c645276b70611fa32fa055b9944934'|\
awk -F\" '/access_token/{print $4}')
The way to do this in jq would be to do
access_token=$(curl --globoff -X POST "https://example.com/token" \
-F 'grant_type=client_credentials' \
-F 'client_id=d86fc9b690963b8dda602bd26f33454r457e9024a4ecccf4c3bbf66ffcbc241b' \
-F 'client_code'='ff148c56118728b62c9f2ew3e4r009a7a1c645276b70611fa32fa055b9944934'|\
jq -r '.access_token' )
I am using this in bash to get and use the token ...
access_tok=$(curl -s -X POST -d "client_id=$(< "$LOGON_HOME/client_id" )&client_secret=$(< "$LOGON_HOME/client_secret")&grant_type=client_credentials" "${TOKEN_URL}" )
access_tok=${access_tok#*access_token\":\"}
access_tok=${access_tok%%\"*}
and later
curl -l -s -O -H "Authorization: Bearer ${access_tok}" "${FILE_URL}"

CURL Sendmail via bash

Hi i've a problem whit this code:
# up here there's more code
echo "Password changed" $(date) > lez.txt
curl -n --ssl-reqd --mail-from "me#lupetto.sh" --mail-rcpt "my mail" -T lez.txt --url smtps://smtp.gmail.com:465 --user "example#gmail.com:password"
If I run the script I get only a empty mail, but if I do this manually I get my mail.
Thanks.
It looks like it's having problems to find the file contents. What about using a here-string to avoid writing a file at all? Change your code to:
curl -n --ssl-reqd --mail-from "me#lupetto.sh" --mail-rcpt "my mail" -T - --url smtps://smtp.gmail.com:465 --user "example#gmail.com:password" <<<"Password changed $(date)"
Notice the echo statement removal, the here-string at the end of the line and the -T - to get the file from stdin.
cURL expects that the file to be sent has a empty line on the 1st row
the script below will do that for you
##!/bin/sh
# USAGE
#
# sh emailfile.sh examplefile.log
file="$1"
echo "" > temp | cat "$1" >> temp && mv temp "$1"
wait
curl --url "smtps://smtp.mail.yahoo.com:465" \
--mail-from "youremail#yahoo.com" \
--mail-rcpt "dest#domain.com" \
--user "youremail#yahoo.com:EmailAccountPassword" \
-T - --silent < "$file"
wait

Resources