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.

  • 1
    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 webcheatsheet.com/php/… – au_stan Aug 21 '12 at 11:46

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


try mailx:

 echo "this is a test mail" | mailx -s "Test mail" you@yourdomain.com

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

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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