Automatic emails using bash and sendgrid

Problem

You would like to be able to automatically send some emails (ie reports) from a linux server, using a bash script that runs in cronjob, without installing an email server on the linux server.

Solution

  • Create an account in Sendgrid and follow the directions for using WebAPI with curl
    here
  • Set up your bash script to use the API key and have a script like the following. If you want to use big or multiple files you will need to use a temporary file for the base64 encoding, as in the example below, as there is a limit in curl.
  • Add your script to crontab
  • curl: (7) Failed to connect to localhost port 80: Connection refused

    Problem

    You would like to access a host set up with nginx after adding your own vhost configuration file in /etc/nginx/conf.d/myfile.smth, but you get Connection refused.

    Solution

    Make sure that your nginx configuration file has the .conf extension as /etc/nginx/conf.d/myfile.conf.

    Copying files between two folders without overwriting existing ones in destination folder.

    Problem

    You would like to copy the contents of a source directory to a destination directory without overwriting existing files on the destination.

    Solution

    You can use

    and you can also use the –dry-run option to test before running it.

    Bash getting the parent folder name from a bash defined variable

    Problem

    You have defined a variable in a bash script (ie MY_FOLDER) but you also want to dynamically get the parent folder (ie MY_FOLDER can have multiple values).

    Solution

    Use the dirname as in the following