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:
- A Linux environment or WSL environment in Windows.
- The
swaks
command installed (sudo apt install swaks
on Debian-based systems). - A Gmail account (or any SMTP server) for sending emails.
- A CSV file (
sample_data.csv
) containing client details. - 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. π¬π£