Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
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/… –  austin Aug 21 '12 at 11:46
add comment

3 Answers

up vote 3 down vote accepted

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

share|improve this answer
add comment

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
share|improve this answer
add comment

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/

share|improve this answer
add comment

Your Answer

 
discard

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.