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 have this plugin on wordpress an already configure class-phpmailer.php to send emails by SMTP. My hosting only accept SMTP emails. When I register in wordpress I receive an email, when I contact anothe user he receives emails. This is ok.

But when I try to use this plugin he doesn't send any emails. Could you help me please? Thank you in advance

 <?php

ini_set('display_errors',true);
ini_set('memory_limit', '128M');

require dirname(__FILE__).'/../../../wp-config.php';    

$db = new PDO ('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
function update_date_format(){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (strpos($lang, 'pt') > 0){
    $date_format = 'd/m/Y'; // 25/12/2011
}else{
    $date_format = 'd/m/Y'; // 12/25/2011
}
update_option('date_format',$date_format);
}


// get config
$site_url  = get_option ('siteurl');
$from      = get_option ('admin_email');
$blog_name = get_option ('blogname');
$logo      = get_option ('cp_logo');
$date_format = get_option ('date_format');
$link_edit = $site_url.'/'.get_option ('cp_edit_item_url').'/?aid=';

$subject   = get_option ('adsstatsemail_subject');
$header    = get_option ('adsstatsemail_header');
$footer    = get_option ('adsstatsemail_footer');
$campaign  = 'utm_source=Weekly%20Ads%20Stats&utm_medium=email&utm_campaign='.urlencode(get_option ('adsstatsemail_campaign'));
$ad_title  = get_option ('adsstatsemail_adtitle');
$ad_week   = get_option ('adsstatsemail_adweek');
$ad_total  = get_option ('adsstatsemail_adtotal');
$ad_expire = get_option ('adsstatsemail_adexpire');
$ad_edit   = get_option ('adsstatsemail_adedit');

update_date_format();
$week_start = time()-(86400*6);


// replace macros
$header = str_replace ("\n\n", "<br/>", $header);
$header = str_replace ('{date_from}', date($date_format, $week_start), $header);
$header = str_replace ('{today}', date($date_format), $header);
$header = str_replace ('{site_link}', "<a href=\"{$site_url}?{$campaign}\">{$site_url}</a></p><br>", $header);
$header = str_replace ('{site_logo}', "<a href=\"{$site_url}?{$campaign}\"><img src=\"{$logo}\" border=\"0\"/></a></p><br>", $header);

$footer = str_replace ("\n\n", "<br/>", $footer);
$footer = str_replace ('{date_from}', date($date_format, $week_start), $footer);
$footer = str_replace ('{today}', date($date_format), $footer);
$footer = str_replace ('{site_link}', "<a href=\"{$site_url}?{$campaign}\">{$site_url}</a>", $footer);
$footer = str_replace ('{site_logo}', "<a href=\"{$site_url}?{$campaign}\"><img src=\"{$logo}\" border=\"0\"/></a>", $footer);


// fetch ad info
$sql = "
    SELECT 
        p.ID,
        p.post_title,
        p.post_date,
        pm.meta_value AS expireDate,
        u.user_email,
        u.display_name,
        t.postcount AS total,
        SUM(d.postcount) AS week
    FROM 
        wp_posts as p
        INNER JOIN wp_postmeta AS pm ON p.ID=pm.post_id AND pm.meta_key = 'cp_sys_expire_date'
        INNER JOIN wp_users AS u ON u.ID=p.post_author
        INNER JOIN wp_cp_ad_pop_total AS t ON t.postnum=p.ID
        INNER JOIN wp_cp_ad_pop_daily AS d ON d.postnum=p.ID AND d.time >= DATE_SUB(NOW(), INTERVAL 1 WEEK) 
    WHERE
        p.post_type = 'ad_listing'
    GROUP BY p.ID
    ORDER BY u.ID
";

$sth = $db->query ($sql);

$list = array ();
while ($row = $sth->fetch (PDO::FETCH_ASSOC)) {

    // remove expired ads
    if (strtotime($row['expireDate']) <= $week_start) { 
        continue;
    }

    $mail = $row['user_email'];
    unset ($row['user_email']);

    $list[$mail][] = $row;
}


// prepend header
$header = "
 <html>
 <head>
 <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>
 </head>
 <body>
 {$header}
 <table>
<tr>
    <td><b><font face=\"Arial\" size=\"2\">{$ad_title}</font></b></td>
    <td><b><font face=\"Arial\" size=\"2\">{$ad_week}</font></b> &nbsp;</td>
    <td><b><font face=\"Arial\" size=\"2\">{$ad_total}</font></b> &nbsp;</td>
    <td><b><font face=\"Arial\" size=\"2\">{$ad_expire}</font></b></td>
    <td></td>
</tr>";

// footer
$footer = "
 </table>
 {$footer}
 </body>
 </html>";


$count = 0;

foreach ($list as $email => $ads) {

    $html = $header;

    foreach ($ads as $ad) {

        $ad['expireDate'] = date($date_format, strtotime ($ad['expireDate']));
        $ad['post_title'] = utf8_encode($ad['post_title']);

        $html .= "
<tr>
    <td><font face=\"Arial\" size=\"2\">{$ad['post_title']} &nbsp;</font></td>
    <td><font face=\"Arial\" size=\"2\">{$ad['week']}</font></td>
    <td><font face=\"Arial\" size=\"2\">{$ad['total']}</font></td>
    <td nowrap><font face=\"Arial\" size=\"2\">{$ad['expireDate']}</font></td>
    <td nowrap><font face=\"Arial\" size=\"2\"><a href=\"{$link_edit}{$ad['ID']}&{$campaign}\" target=\"_blank\">{$ad_edit}</a></font></td>
</tr>";

    }

    $html .= $footer;

    $headers = "From: {$blog_name} <{$from}>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
    mail ("{$ad['display_name']} <$email>", $subject, $html, $headers);

    echo "$email\n";
    $count++;
}

echo "\nDone ($count emails).\n\n";
share|improve this question
    
The code you posted does not use PHPMailer at all. It sends the mail using plain ol' PHP mail(). See the seventh line from the bottom... –  Johannes Pille Mar 20 '13 at 18:46
    
And its possible change this? How? –  RAN Mar 20 '13 at 19:10

1 Answer 1

Replace the line

mail ("{$ad['display_name']} <$email>", $subject, $html, $headers);

with

wp_mail("{$ad['display_name']} <$email>", $subject, $html, $headers);

and install the WP_Mail_SMTP plugin which will use SMTP instead of the php mail() function.

share|improve this answer
    
Thank you, Thank you, Thank you! –  RAN Mar 20 '13 at 20:19
    
One more question. Is posssible make this script send only 700 email / Hour. Could you help me please? –  RAN Mar 20 '13 at 20:42
    
That's more of server configuration question really. You could probably hack something using the Transients API or use an external server which doesn't have SMTP limits, e.g. SendGrind –  MartinB Mar 20 '13 at 22:35
    
Also be sure to mark this as the answer if it has answered your original question! Cheers. –  MartinB Mar 20 '13 at 22:37

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.