Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like some advice on how best to send an email after running a SQL query. Every day, I would like to run a PHP/SQL script to identify users who should be emailed. After this runs, I would like to email those users and attach a file for each user (which is the output of another PHP/SQL process).

My approach thus far is to use use cron to call the php script and to write the csv output to a unique temporary directory for each user. However, I'm not sure how best to email the users this data.

I'm using a CentOS server and using PostGreSQL for my database.

share|improve this question
just use mail. mail($to,$subject,$message,$headers); if you need to work with an attachment, you'll need to work on your header. see… – au_stan Aug 21 '12 at 11:46
up vote 3 down vote accepted

Best way is to send the mail via your php code ( Cron can only have one mail recipient per crontab entry (Override MAILTO for a single crontab entry).

share|improve this answer

try mailx:

 echo "this is a test mail" | mailx -s "Test mail"

If this does not work maybe you do not have a local mail server available. In this case, install the ssmtp package, and configure /etc/ssmtp/ssmtp.conf. You need to provide a real mail server and the local domain name:

# /etc/ssmtp.conf -- a config file for sSMTP sendmail.

# The place where the mail goes.

# Where will the mail seem to come from?

# The full hostname
share|improve this answer

As you said, its PHP/SQL script you can use the php mail function to send email. You can also attach the file that you generated.

Here how you can send the email in PHP:

If you are sending a lot of mails and need to optimize it, then read this one:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.