Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 = "
<html>
    <head>
        <title>Alton Bible Church - Broken Link</title>
            <style type=\"text/css\">
                * {
                    font-family:Arial;
                }
                table {
                    background:#eee;
                    padding:15px;
                }
                #table {
                    width:500px;
                    overflow:scroll;
                }
                td {
                    border: 1px solid #aaa;
                    padding:10px;
                }
        </style>
    </head>
    <body>
        <p>There's a broken link on My Website</p>
        <div id=\"table\">
            <table>
                <tr>
                    <td>Broken Link</td><td>Referring Link</td>
                </tr>
                <tr>
                    <td><a href=\"" . $broken . "\">" . $broken . "</a></td><td><a href=\"" . $referrer . "\">" . $referrer . "</a></td>
                </tr>
            </table>
        </div>
    </body>
</html>";
$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.

Thanks!

share|improve this question

3 Answers 3

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.


//SENDS 404 EMAIL TO ADMIN
function email_admin($location){
    $blname=get_option('blogname');
    $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";

    @mail($admemail,$mailsubj,$mailbody,$mailhead);
}

function mail_me_errors(){
    global $wp_query;
    $location=$_SERVER['REQUEST_URI'];
    if ($wp_query->is_404){
        email_admin($location);
    }
}
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

 
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.