• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved Cronjob to import addresses to mailing list

dennis_81

Basic Pleskian
Hello,
I am looking for a way to import a list of eMail addresses into the mailinglist by job.

The mail addresses are in a SQL database. But I have understood so far that there is no direct way to do this.
But I could of course write this list via PHP into a .txt or similar.

Can I import this list automatically by task, e.g. every hour?

I know the solution is somewhere here:

Code:
plesk bin maillist --update MailinglistName -members add:[email protected] -domain example.com

But how do I get Plesk to import content from a text file here, and then how exactly do I run this command via cronjob?

Glad about any hint.

Thank you
Dennis
 
One possible approach could be this:

Create a .txt file that contains all the mail addresses that should be added, example "list.txt":
[email protected]
[email protected]
[email protected]

Then run a small script to import those addresses:
Bash:
for address in `cat list.txt` ; do domain=$(echo $address | sed 's/^\(.*\)@\(.*\)$/\2/') ; plesk bin maillist --update MailinglistName -members add:$address -domain $domain ; done

There are probably more elegant ways to do it but this quick hack should do the job....
 
Hello Monty,

thank you for your answer. Sounds good so far.
I am unfortunately a complete newbie in Plesk. Therefore still a few questions about it:

When I generate the text file, it is in the root of the website. How do I call this text file from the script then?

Can I call your command directly as a cronjob in the Plesk GUI? Or what do I have to do to schedule this script as a task?
 
If we assume that your text file is /var/www/vhosts/example.com/httpdocs/list.txt then:

You could create a bash script /var/www/vhosts/example/import.sh with the content:
Code:
#!/bin/bash

for address in `cat /var/www/vhosts/example.com/httpdocs/list.txt` ; do
    domain=$(echo $address | sed 's/^\(.*\)@\(.*\)$/\2/')
    plesk bin maillist --update MailinglistName -members add:$address -domain $domain
done

Then, make that script executable: chmod +x /var/www/vhosts/example/import.sh

And finally, create a scheduled task in Plesk as root in Tools & Settings -> Scheduled Tasks with the command:
/var/www/vhosts/example/import.sh
 
Hi @Monty


thank you very much for the detailed explanation.
unfortunately it took a moment until i could test it but I think this is how it works.
However, I still have a small permission problem:

If I execute the command directly in SSH terminal with SUDO, it works great.
But if I create a task with my Plesk Administrator, I get the following error:

1643471112431.png


Code:
/var/www/vhosts/mydomain.com/bash/import.sh: line 5: plesk: command not found

My bash script looks like this

Code:
#!/bin/bash

for address in `cat /var/www/vhosts/mydomain.com/httpdocs/list.txt` ; do
    domain=$(echo $address | sed 's/^\(.*\)@\(.*\)$/\2/')
    plesk bin maillist --update testverteiler -members add:$address -domain mydomain.com
done

I suspect this is because I can't access the vhosts subdirectory even with my Plesk Administrator.

But when I try to create and start the task directly with my domain user, I get the same error.

What do I have to change so that this also works as a task?

Thank you very much.

Best regards
Dennis
 
Replace
Bash:
plesk bin maillist --update testverteiler -members add:$address -domain mydomain.com

with
Bash:
/usr/local/psa/bin/maillist --update testverteiler -members add:$address -domain mydomain.com
 
Back
Top