WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a plugin that emails the site webmaster when I go to the 404 page. The script checks if the referring link is on our site, and then sends an email to the webmaster.

$message = "
        <title>Alton Bible Church - Broken Link</title>
            <style type=\"text/css\">
                * {
                table {
                #table {
                td {
                    border: 1px solid #aaa;
        <p>There's a broken link on My Website</p>
        <div id=\"table\">
                    <td>Broken Link</td><td>Referring Link</td>
                    <td><a href=\"" . $broken . "\">" . $broken . "</a></td><td><a href=\"" . $referrer . "\">" . $referrer . "</a></td>
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: My Website <no-reply@mysite.net>' . '\r\n';
mail("Webmaster <webmaster@mysite.net>", "My Website - Broken Link", $message, $headers);

The problem is, the email is not being sent. Does WordPress not allow me to use mail()?

The problem shouldn't be with the server.


share|improve this question
up vote 1 down vote accepted

Use wp_mail(), not just mail(). It is a wrapper for PHPMailer, a class that takes care for many problems the native mail() function often gets.

See my plugin 404 Tools for how to use it in this case. To send an HTML email see Milo’s answer to a related question.

share|improve this answer
It still doesn't send the email. – Caleb Oct 21 '12 at 17:12
Update the code in your question, enable the debug mode, and add all errors you get to the question too. – toscho Oct 21 '12 at 17:24
Test my plugin. If it works there is a glitch your PHP code. – toscho Oct 21 '12 at 17:37
Still nothing. (I am using XAMPP right now to develop.) – Caleb Oct 21 '12 at 17:40
You have to set up Mercury in XAMPP. Or test it on a real Linux server. – toscho Oct 21 '12 at 17:53

wordpress doesn't block the php mail() function. One error that i can see is that '\r\n' should be in double quotes like this "\r\n"

Even though mail() works, it's a good practice to use wordpress functions, here wp_mail()

share|improve this answer

Just put this in your functions.php and it should be OK.

function email_admin($location){
    $admemail = get_option('admin_email');
    $ipaddress = $_SERVER["REMOTE_ADDR"];
    $checkip = "http://www.projecthoneypot.org/ip_".$ipaddress;
    $mailhead  = "MIME-Version: 1.0\r\n";
    $mailhead .= "Content-type: text/plain; charset=UTF-8\r\n";
    $mailhead .= 'From: "' . $blname . '" \r\n";
    $mailsubj= $blname.': 404 error';
    $mailbody='Someone wanted to go to '.$_SERVER['SERVER_NAME'].$location." but you haven't created this page yet. Maybe you can have a look and see if anything needs to be fixed.\r\n
Their IP address is: ".$ipaddress."\r\n\nIn case you want to check if these are THE BAD GUYS, you can do it at $checkip";


function mail_me_errors(){
    global $wp_query;
    if ($wp_query->is_404){
add_action('get_header', 'mail_me_errors');
share|improve this answer
Please add more explanation than just "put this in your functions.php". An answer should cover the "why" as well. – kaiser Nov 6 '12 at 0:10

Your Answer


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.