Hai,

How to run automatic mail sending in php. I have a code that generates a report from my database to my e-mail id while run my php file. But I am in need of my php file should automatically run and the report sent to my mail.

Thanks in advance.

$send_to = "foo@bar.com";
$subject = "Test";
$message = 'This is a test.';
$headers = "From: arijith@mysite.com";
$headers .= "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers);

This code adds two headers. You could add others or comment out the second one. If multiple headers are present they need to be separated by a carriage return/line feed as shown.

You can also set the default From: in your php.ini.

If you're sending mail using the Pear mail classes or some other method, you'll have to look at the API to determine how to specify the From: field.

Member Avatar for diafol

To send automatically, you can set up a cron job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.