How can I send an email in my plugin?

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' . 'rn';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . 'rn';
$headers .= 'From: My Website <no-reply@mysite.net>' . 'rn';
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()?

Read More

The problem shouldn’t be with the server.

Thanks!

Related posts

Leave a Reply

3 comments

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

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

  2. 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.0rn";
        $mailhead .= "Content-type: text/plain; charset=UTF-8rn";
        $mailhead .= 'From: "' . $blname . '" rn";
        $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.rn
    Their IP address is: ".$ipaddress."rnnIn 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');