So you need to create a lot of auto responders in cPanel. But cPanel's interface only allows it to be created one at a time. At least that's what I've seen with the latest version (11.32 as of this moment). So I wrote a quick Python and bash script to quickly create the files necessary from a template and a list of email addresses. Since this is in connection with my client's recent domain change, they required that the email contain the new email address of whoever is sending the auto response. Plus they had 240+ accounts and I wasn't about to do it all manually.
Requirements
First of all, you need to have a way of uploading and making sure the permissions of the uploaded files are correct. I have WHM and shell access, so that made things easy. I have no idea if it's the same with just a plain cPanel account.
And since this is a Python and bash script, Python and bash. There's nothing fancy about my scripts.
Step 1: Get the list of emails
I just did a:
ls -1 ~/mails/domainname.com/ > ~/email-list.txt
Then I just edited that file and removed the trailing slash.
Step 2: The templates
So I had two template files. One contains the email, the other is a json file that contains the start to end duration to send the auto response, as well as the duration before sending it again to a recipient.
For the json file, I just used the default start to end of none (meaning forever) and the duration of 8 hours (28800 seconds). So here's json-template.txt:
{"start":null,"stop":null,"interval":"28800"}
Then I created the template email. This requires the "From", "Content-type" with "charset" (set to utf-8), "Subject", and the actual content of the message. So it looks like this (we'll call this email-template.txt):
From: "%from%" <EMAILVAR@domainname.com>
Content-type: text/plain; charset=utf-8
Subject: This is the subject
Hi, from now on please email me at EMAILVAR@newdomain.com. Thanks. Bye.
So note that it contains the string "EMAILVAR". That's what I'll be replacing using the Python script.
Step 3: The Python script
So the script is simple. It just replaces the EMAILVAR with whatever you provide as an argument. In fact it's so simple, that this could probably be done with just plain bash. Why did I use Python? Because I already new how to do it that way. So here's, uh, fffuuu.py:
#!/usr/bin/python
#
import re
import sys
# This is the parameter.
strEmail = sys.argv[1]
# Email template here.
fileContent = 'email-template.txt'
##################################################
fp = open(fileContent)
strReplace = fp.read()
fp.close()
##################################################
strReplace = re.sub("EMAILVAR", strEmail, strReplace)
##################################################
print strReplace
It just outputs the template with replaced string straight to the console. Then…
Step 4: The bash script
… I just parse it using a while loop in bash. So, parse.sh:
while read line
do
EMAIL=`echo $line`
./fffuuu.py "$EMAIL" > output/"$EMAIL@domainname.com"
cp json-template.txt output/"$EMAIL@domainname.com.json"
done < email-list.txt
So we loop through email-list.txt, use the email-template.txt in the Python script, and out that to a file in an "output" directory. Then we copy our json-template.txt file with the same email address.
Step 5: Upload the files to .autorespond
So now you just upload all the created files and put them in ~/.autorespond/. Make sure they get the permission of the account uploaded to. So that's:
chmod user:user ~/.autorespond/*
Step 6: The only manual part
So, the catch is this doesn't work immediately. The "autorespond" stuff isn't necessarily enabled in the domain unless a user has created one before. The file to be edited is /etc/valiases/domainname.com. Now I wasn't about to edit this file manually as I wasn't sure if that might cause problems with forwarding addresses.
So what I did instead was, go to the cPanel Web UI, then go to the Auto Responders section. Then seeing as all the auto responders I created were there, just clicked "Edit" on every one of them and clicked the "Create/Modify" button on the bottom. Browser tabs worked wonders here.
Yeah this is the only lame solution I found for all this. I could have probably have scripted inserting the "autorespond" stuff in the configuration file itself but was pressed for time.
Conclusion
I wish cPanel could create a domain-wide auto responder option. This would be useful.
Also, I named the Python script fffuuu.py because, like I said, I was pressed for time.
Comments