send mail function

This commit is contained in:
MorganGeek 2021-06-07 09:57:04 +02:00
parent a9c4b9b782
commit db6430d3df
3 changed files with 96 additions and 0 deletions

24
dot_scripts/mail.tpl Normal file
View File

@ -0,0 +1,24 @@
From: "#FROM_NAME" <#FROM_MAIL>
To: "#TO_NAME" <#TO_MAIL>
Subject: Convert
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MULTIPART-MIXED-BOUNDARY"
--MULTIPART-MIXED-BOUNDARY
Content-Type: multipart/alternative; boundary="MULTIPART-ALTERNATE-BOUNDARY"
--MULTIPART-ALTERNATE-BOUNDARY
Content-Type: text/plain; charset="UTF-8"
Hello #TO_NAME
--MULTIPART-ALTERNATE-BOUNDARY
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello #TO_NAME</div>
--MULTIPART-ALTERNATE-BOUNDARY--
--MULTIPART-MIXED-BOUNDARY
Content-Type: application/octet-stream; name="#ATTACHMENT_NAME"
Content-Disposition: attachment; filename="#ATTACHMENT_NAME"
Content-Transfer-Encoding: base64

33
dot_scripts/sendmail.sh Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
source "$HOME/.scripts/secrets.sh"
function file_to_mail() {
local source_file="$1"
local attachment_name=$(basename "$1")
local target_file="$source_file-encoded"
base64 "$source_file" >"$target_file"
local target_file_content
target_file_content=$(\cat "$target_file")
local mail_template="$HOME/Code/dotfiles/dot_scripts/mail.tpl"
local mail_from="$PRINCIPAL_ACCOUNT_EMAIL"
local mail_to=${2:-"$mail_from"}
local name_from="$PRINCIPAL_ACCOUNT_FULLNAME"
local name_to="$name_from"
\cat "$mail_template" |
sed "s/#FROM_NAME/$name_from/g" |
sed "s/#FROM_MAIL/$mail_from/g" |
sed "s/#TO_NAME/$name_to/g" |
sed "s/#TO_MAIL/$mail_to/g" |
sed "s/#ATTACHMENT_NAME/$attachment_name/g" >/tmp/mailwithattachement.txt
# sed "s/#ATTACHMENT_CONTENT/$target_file_content/g" >/tmp/mailwithattachement.txt
# sed "s/#ATTACHMENT_CONTENT/$target_file_content/g" >/tmp/mailwithattachement.txt
echo "\n$target_file_content\n" >>/tmp/mailwithattachement.txt
echo "\n--MULTIPART-MIXED-BOUNDARY--" >>/tmp/mailwithattachement.txt
curl -v --ssl-reqd --url 'smtps://smtp.gmail.com:465' --user "$GMAIL_APP_USER:$GMAIL_APP_PASSWORD" --mail-from "$mail_from" --mail-rcpt "$mail_to" --upload-file /tmp/mailwithattachement.txt
}

View File

@ -179,6 +179,45 @@ function suggest_aliases() {
done < <(top_commands_full "" "$search_input_size" | awk '{ $1=""; $2=""; $3=""; print}' | awk 'NF' | awk '{$1=$1};1' | awk -v COUNT=1 'NF>COUNT' H "-$search_input_size") done < <(top_commands_full "" "$search_input_size" | awk '{ $1=""; $2=""; $3=""; print}' | awk 'NF' | awk '{$1=$1};1' | awk -v COUNT=1 'NF>COUNT' H "-$search_input_size")
} }
# Download management
function file_to_kindle() {
local source_file="$1"
if [[ "$source_file" =~ .*.epub$ ]]; then
if epub2mobi "$source_file" 2>/dev/null; then
source_file=$(echo $source_file | sed 's/.epub$/.mobi/')
fi
fi
file_to_mail "$source_file" "$KINDLE_ACCOUNT_EMAIL"
}
function getabook() {
local searchterm="$1"
if searchbook "$searchterm"; then
echo "new downloaded file"
grepsearchterm=$(echo "$searchterm" | sed 's/ /.*/g')
if find ~/Downloads/Books -mmin -2 | grip "$grepsearchterm.*(epub|mobi|pdf)$"; then
local matching_file=$(find ~/Downloads/Books -mmin -2 | grip "$grepsearchterm.*(epub|mobi|pdf)$" | head -1)
local downloaded_file=$(basename "$matching_file")
cd "$HOME/Downloads/Books" &>/dev/null || exit
success "file downloaded in your Books folder : $downloaded_file"
if file_to_kindle "$downloaded_file" 2>/dev/null; then
success "file sent to your kindle, please review your emails to approve the transfer"
mailperso
else
error "could not send your file $downloaded_file to your email address, please check your folder"
open ~/Downloads/Books
fi
else
error "could not find a matching file in your download folder"
fi
else
error ":'( the automatic download failed for $1, does this book exists ? check yourself or retry"
browse "https://libgen.lc/search.php?req=$1"
fi
}
# Mail management
source $HOME/Code/dotfiles/dot_scripts/sendmail.sh
# Sound management # Sound management
# Inspired by https://apple.stackexchange.com/a/213048/231885 for switching devices # Inspired by https://apple.stackexchange.com/a/213048/231885 for switching devices
# and https://coderwall.com/p/22p0ja/set-get-osx-volume-mute-from-the-command-line for volume management # and https://coderwall.com/p/22p0ja/set-get-osx-volume-mute-from-the-command-line for volume management