33 Votes

PHP: Send Output of a Script by Mail

Tutorial by Stefan Trost | Last update on 2023-01-19 | Created on 2015-02-04

In the following tutorial, I would like to show you a way, how to send the output of a PHP script via e-mail. This can be helpful, for example, if you would like to get some notification about the work and result of a cronjob.

We will catch the output of the script by using Output Buffering and we will use the PHP function mail() for the mailing. A possible implementation can look like this, for example:

// mail parameter
$recp = 'recipient@example.com';
$send = 'Sender Name <sender@example.com>';
$subj = 'Subject of the Mail';

// start output buffering
// create some output
echo "TEST";  
// read out buffer and store it into variable
$msg = ob_get_contents();  
// stop output buffering
// send content by mail
$header  = 'TO: ' . $recp. "\r\n";
$header .= 'FROM: ' . $send . "\r\n";
mail($recp, $subj, $msg, $header);

Now let's take a look at what we're doing here step by step:

General Variables

First of all, we are defining some variables such as recipient, sender and subject for sending the e-mail, that are used in the further course of script. We do this at the beginning so that we can quickly adjust the script later without having to search the whole script for that data. E-mail addresses can be specified either with or without name. In the code we see an example of both variants.

Buffer Output

Following, we are starting to the output buffering with ob_start(). From this time on, every potential output (for example via echo, var_dump or displayed error messages) will be written into an internal buffer instead of being sent as output. Accordingly, these output would aslo not be visible in the browser. More information and details about output buffering can be found here. After the output buffering is started, arbitrary outputs can follow. In our example above, we only echo the string "TEST", but we could also execute any other code including script calls.

After processing the script, we can read out the internal buffer using the function ob_get_contents(). Here, we are storing the results into the variable $msg and we are stopping the output buffering with ob_get_contents(). Starting with this moment everything works again as usual and, for example,  any output would be sent to the browser again instead of written into the buffer.

Send E-Mail

Now that we have all we need, we only have to send our e-mail. For this, we build an email header and otherwise use the variables for sender, recipient and subject that we have defined above. Sending e-mails via PHP is described here in more detail, if someone would like to adjust the script at this point.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.