Sending an email via cron

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.

• 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.

root=postmaster
# The place where the mail goes.
mailhub=mail.yourdomain.com

# Where will the mail seem to come from?
rewriteDomain=yourdomain.com

# The full hostname
hostname=server.yourdomain.com


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:
http://www.finalwebsites.com/forums/topic/php-e-mail-attachment-script

If you are sending a lot of mails and need to optimize it, then read this one:
http://www.activecampaign.com/help/how-to-optimize-your-server-mail-server-and-software/