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 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


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
    $date_format = 'd/m/Y'; // 12/25/2011

// 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');

$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 = "
        pm.meta_value AS expireDate,
        t.postcount AS total,
        SUM(d.postcount) AS week
        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) 
        p.post_type = 'ad_listing'

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

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

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

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

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

// prepend header
$header = "
 <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>
    <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>

// footer
$footer = "

$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 .= "
    <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>


    $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";

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

Replace the line

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


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 – MBL Mar 20 '13 at 22:35
Also be sure to mark this as the answer if it has answered your original question! Cheers. – MBL Mar 20 '13 at 22:37

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.