Plesk – extract all email addresses from database

I recently needed to email everybody on my Plesk server. Traditionally I’ve done this by maintaining an “all@domain.com” list for each subscription but of course that requires constant maintenance.

So instead I did a bit of searching, amended what I found (on the plesk forum I believe), and came up with this (which requires ssh access) –

# log into mysql
mysql -uadmin -p`cat /etc/psa/.psa.shadow`

# Once logged into mysql 
use psa;
select concat(mail.mail_name,"@",domains.name) as address from mail,domains,accounts where mail.dom_id=domains.id and mail.account_id=accounts.id order by address INTO OUTFILE '/tmp/emailaddresses.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
exit

# This will create a file /tmp/emailaddresses.csv, which you can email yourself as follows
mail me@domain.com < /tmp/emailaddresses.csv

# and then delete the source file for good measure!
rm /tmp/emailaddresses.csv -f