Como gerar uma imagem de assinatura de e-mail aleatória.

Introdução

O script a seguir pode ser chamado por meio do Linux Cron para selecionar e copiar uma imagem aleatória de um conjunto predefinido de imagens. A imagem copiada com um nome fixo pode ser anexada a uma assinatura de e-mail. Além disso, um link pode ser usado na imagem.

Etapas para fazer isso funcionar:

  • Salve o script como signature.sh e altere o url ao qual a imagem está vinculada por meio do argumento shell:
sh path-to-file/signature.sh  "http://your-website.de"

ou coloque o url em um arquivo chamado link_url.txt (deve ser usado se o seu hoster não permitir argumentos cron)

  • Coloque algumas imagens em uma subpasta chamada “assinaturas”.
  • Ligue para o seu roteiro via Cron
  • Use-o em suas assinaturas de e-mail como este:
<a href="http://www.your-website.de/path-where-you-place-the-script/link.php">
<img border="0" src="http://www.your-website.de/path-where-you-place-the-script/signature.jpg" alt="" /></a>
  • Se algo der errado, verifique os comentários no script.

O roteiro:

#! /bin/bash
# relative path to signatures
signaturepath
="signatures"
# absolute path to this script
script
=$(readlink -f $0)
scriptpath
=$(dirname $script)
# absolute path to signatures
dir
="${scriptpath}/${signaturepath}"
# list directory (one file per line), pass it to sort (random), pass it to head (first line)
file
=$(/bin/ls -1 ${dir} | sort --random-sort | head -1)
# get the real absolute path to this file with readlink
path
=$(readlink --canonicalize "$dir/$file")
# echo "The file is: $path"
# do nothing if file is not present, happens when the signatures folder is empty
if [ -n "$file" ]; then
# copy the random file to script root with new name
cp $path $
{scriptpath}/signature.jpg
# default email link url
param
="http://www.your-website.de"
# override default email link url with custom one from txt file
# a) override by file link_url.txt if exists
if [ -f ${scriptpath}/link_url.txt ]; then
param
=$(cat ${scriptpath}/link_url.txt)
fi
# override default email link url with custom one from shell argument
# b) more important if both are present: override by argument if exists
if [ -n "$1" ]; then
param
=$1
fi
# modify the link.php to point to the current link url
echo
"<?php header("Location:${param}""); exit; ?>"" > ${scriptpath}/link.php
fi