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.
    #!/bin/bash
    
    # Email setup
    SENDGRID_API_KEY="your_sendgrid_api_key"
    FILENAME_ATTACH="title_of_your_attachment"
    FILENAME_ZIP="the_path_to_your_zip_file"
    FILENAME_BASE64_TMP="the_path_to_temporary_base64_encoding"
    EMAIL_TO="email_to_address"
    EMAIL_SUBJECT="email_subject"
    EMAIL_FROM="email_from_address"
    EMAIL_MESSAGE="your_email_message"
    
    function email_exports()
    {
      FILENAME_BASE64=$(base64 -w0 $FILENAME_ZIP);
    
      REQUEST_DATA='{"personalizations": [{
                            "to": [{ "email": "'"$EMAIL_TO"'" }],
                            "subject": "'"$EMAIL_SUBJECT"'"
                    }],
                    "from": {
                            "email": "'"$EMAIL_FROM"'"
                            },
                    "content": [{
                            "type": "text/plain",
                            "value": "'"$EMAIL_MESSAGE"'"
                            }],
                    "attachments": [{
                            "content": "'"$FILENAME_BASE64"'",
                            "filename": "'"$FILENAME_ATTACH"'"
                            }]
      }';
    
      # We need to store the base64 locally as the text
      # is too big for sending directly with curl
       echo $REQUEST_DATA > $FILENAME_BASE64_TMP
    
       curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \
            -H "Authorization: Bearer $SENDGRID_API_KEY" \
            -H "Content-Type: application/json" \
            -d "@$FILENAME_BASE64_TMP";
    
       rm $FILENAME_BASE64_TMP
    }
    
    email_exports
  • Add your script to crontab
  • Using Postfix to send out emails from development environment in Rails

    Problem

    You want to be able to send emails from your development enironment using Postfix in your (K)Ubuntu pc.

    Solution

    First you would need to install postfix:

    sudo apt-get install postfix

    and then you would need to change an option in postfix to not use tls, so change /etc/postfix/main.cf:

    sudo vi /etc/postfix/main.cf

    and change the smtpd_use_tls from yes to no:

    smtpd_use_tls=no

    restart your postfix server:

    sudo /usr/sbin/postfix reload

    and then setup your config/development.rb as follows:

    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      address:                 "127.0.0.1",
      port:                    25,
      enable_starttls_auto:    false
    }
    

    Command line mail message with postfix in (K)ubuntu

    Problem

    You have installed postfix in your local development machine (sudo apt-get install postfix), and you want to test sending emails from the command line using mail email_address@someone.com

     

    Solution

    You would first need to install the mailutils package:

    sudo apt-get install mailutils
    

    then you can send an email by:

    mail email_name@example.com
    CC: (leave blank)
    Subject: Test subject
    Main message body
    

    and you can send it by pressing Ctrl+D

    Dreamhost – SquirrelMail – old-messages folder

    Problem
    You have left a message in a squirrel mail account in dreamhost for a long time and it gets archived by dreamhost. According to the dreamhost you should be able to get to your archived messages, by looking at the old-messages folder.
    The problem is that just by logging in to squirrel mail the folder doesn’t get displayed on the left hand side.

    Solution
    Log on to squirrel mail and go to ‘Folders’ menu at the top.
    In the ‘Create Folder’ put ‘test’ or anything that you would like in the text box, and make sure that in the dropdown box you select ‘INBOX.old-messages’, so your newly created folder is a subfolder of the ‘inbox/old-messages’ folder.
    Create the subfolder, refresh the page, and in your left hand the old-messages folder should appear.