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 <[email protected]>' . '\r\n';
mail("Webmaster <[email protected]>", "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.


3 Answers 3


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.

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


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');
  • Please add more explanation than just "put this in your functions.php". An answer should cover the "why" as well.
    – kaiser
    Nov 6, 2012 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.