Automating Monthly PDF Report Delivery to Clients Using `swaks` in Linux πŸ“§πŸ“„

A close-up photo of a smartphone displaying popular apps like Google and Mail.

In today’s fast-paced business environment, automating repetitive tasks can significantly improve operational efficiency. One such task is sending monthly reports to clients in PDF format. Did you know that, you can leverage the power of theΒ swaksΒ command to automate this process? In this blog post, I’ll walk you through how to useΒ swaksΒ to send PDF reports to clients automatically, based on a CSV file containing client details. Let’s dive in! πŸš€


Why Automate Email Delivery? πŸ€”

Manually sending emails to multiple clients every month can be time-consuming and error-prone. Automating this process ensures:

  • Consistency: Every client receives the same well-formatted email.
  • Efficiency: Saves time and reduces the risk of human error.
  • Scalability: Easily handle a growing number of clients without additional effort.

Prerequisites πŸ› οΈ

Before we begin, ensure you have the following:

  1. A Linux environment or WSL environment in Windows.
  2. The swaks command installed (sudo apt install swaks on Debian-based systems).
  3. A Gmail account (or any SMTP server) for sending emails.
  4. A CSV file (sample_data.csv) containing client details.
  5. A folder named Final containing the PDF reports.

Step 1: Prepare Your CSV File πŸ“‚

Your sample_data.csv file should look like this:

PDF filename,Customer Name,Customer ID,Customer Email
john_doe.pdf,John Doe,12345678,john_doe@gmail.com
jane_smith.pdf,Jane Smith,87654321,jane_smith@yahoo.com
  • Column 1: PDF filename (e.g., john_doe.pdf).
  • Column 4: Customer Email (e.g., john_doe@gmail.com).

Step 2: Create the Email Body Template πŸ“

Save the following content in a file named EmailBody.txt:

Dear Sir/Madam,

I hope this message finds you well.

Please find attached the monthly report for your review. We kindly ask that you review the document thoroughly and report any discrepancies to our office within 14 days from the date of this email.

Should you have any questions or require further clarification, please do not hesitate to reach out.

Thank you for your attention to this matter.

Best regards,
[Company Name]

Step 3: Write the Bash Script to Send Emails πŸ–₯️

Create a script named SendEmail.sh to send emails using swaks:

#!/bin/bash
# Filename: SendEmail.sh

swaks \
--from [your_gmail_account]@gmail.com \
--to $1 \
--server smtp.gmail.com:587 \
--tls \
--auth LOGIN \
--auth-user '[your_gmail_account]@gmail.com' \
--auth-password '[your_gmail_app_password]' \
--header 'Subject: Monthly Report Attached - Action Required' \
--body EmailBody.txt \
--attach Final/$2

Replace [your_gmail_account] and [your_gmail_app_password] with your Gmail credentials. If you’re using Gmail, make sure to generate an App Password for secure authentication.


Step 4: Automate Bulk Email Delivery πŸ“€

To send emails in bulk, create another script named SendEmailInBulk.sh:

#!/bin/bash
# Filename: SendEmailInBulk.sh

# Define the input CSV file
CSV_FILE="sample_data.csv"

# Check if the CSV file exists
if [[ ! -f "$CSV_FILE" ]]; then
    echo "Error: CSV file $CSV_FILE not found!"
    exit 1
fi

# Read the CSV file line by line, skipping the header
while IFS=, read -r pdf_filename customer_name customer_id customer_email
do
    # Skip the header line
    if [ "$pdf_filename" != "PDF filename" ]; then
        # Trim any leading/trailing whitespace from the variables
        pdf_filename=$(echo "$pdf_filename" | xargs)
        customer_email=$(echo "$customer_email" | xargs)

        # Execute the SendEmail.sh script with the extracted parameters
        ./SendEmail.sh "$customer_email" "$pdf_filename"

        # Check if the SendEmail.sh script executed successfully
        if [[ $? -eq 0 ]]; then
            echo "Email sent successfully to $customer_email with PDF $pdf_filename"
        else
            echo "Failed to send email to $customer_email with PDF $pdf_filename"
        fi
    fi
    sleep 5
done < "$CSV_FILE"

This script reads the CSV file, extracts the client’s email and PDF filename, and sends the email using SendEmail.sh.


Step 5: Execute the Script πŸš€

Make the scripts executable:

chmod +x SendEmail.sh
chmod +x SendEmailInBulk.sh

Run the bulk email script:

./SendEmailInBulk.sh

Expected Folder Structure πŸ“‚

.
β”œβ”€β”€ EmailBody.txt
β”œβ”€β”€ Final
β”‚ β”œβ”€β”€ jane_smith.pdf
β”‚ └── john_doe.pdf
β”œβ”€β”€ GenReportInBulk.sh
β”œβ”€β”€ GenReport.sh
β”œβ”€β”€ Part1
β”‚ β”œβ”€β”€ 12345678.pdf
β”‚ └── 87654321.pdf
β”œβ”€β”€ Part2
β”‚ β”œβ”€β”€ 12345678.pdf
β”‚ └── 87654321.pdf
β”œβ”€β”€ Part3
β”‚ β”œβ”€β”€ 12345678.pdf
β”‚ └── 87654321.pdf
β”œβ”€β”€ sample_data.csv
β”œβ”€β”€ SendEmailInBulk.sh
└── SendEmail.sh

Please check out this post to find out the purpose of both GenReport.sh and GenReportInBulk.sh.


Final Thoughts πŸ’‘

Automating the process of sending monthly reports to clients not only saves time but also ensures accuracy and professionalism. By using swaks and a simple bash script, you can streamline this task and focus on more strategic activities.


Have you automated similar tasks in your back office? What tools or techniques do you use to improve operational efficiency? Share your experiences or tips in the comments below! Let’s start a discussion and learn from each other. πŸ’¬πŸ“£

Leave a Comment

Your email address will not be published. Required fields are marked *