Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Send email WordPress

I want to notify campaign creator on new donations via email (camp_email from wp_campaigns). It doesnt work. What's wrong with my code?

foreach ($_REQUEST as $key => $value)
{

$msg.=$key."=>".$value.'<br>';
}


global $wpdb;

$table_name= $wpdb->prefix.'contribution';
$data = $wpdb->get_results("SELECT * FROM $table_name");
Inner Join wp_campaigns ON wp_contribution.cont_camp_id = wp_campaigns.camp_id
WHERE
wp_campaigns.camp_field_anonymous <> '1'";

$arrCustom=split("#",$_REQUEST["custom"]);

$data["cont_camp_id"]=$arrCustom[0];

$data["cont_amt"]=$_REQUEST["mc_gross"];

if($_REQUEST["first_name"] != "" ) {
$data["cont_first_name"]= $wpdb->escape($_REQUEST["first_name"]);
}


if($_REQUEST["last_name"] != "" ) {
$data["cont_last_name"]= $wpdb->escape($_REQUEST["last_name"]) ;
}

if($_REQUEST["payer_email"] != "" ) {
$data["cont_email"]= $_REQUEST["payer_email"] ;
}

if($_REQUEST["campaign_creator_email"] != "" ) {
$data["camp_email"]= $wpdb->escape($_REQUEST["creator_email"]);
}


$data["txn_id"]= $_REQUEST["txn_id"] ;

$data["cont_date"]= date("Y-m-d");

$rows_affected = $wpdb->insert( $table_name, $data );

$msg = get_option( 'paypal_email_msg' );

$from=get_option( 'from_address' );

$email_subject=get_option( 'email_subject' );

mail( $send_mail_to , $email_subject, $msg, $from);

// mail to donor

if($_REQUEST["payer_email"] != "" ) {

$send_mail_to = $_REQUEST["payer_email"];

$msg="Dear ".$_REQUEST["first_name"].",\n\n";

$msg .= get_option( 'paypal_email_msg' );

$msg .="\n\n==== Donation Information ====";
$msg .="\nConfirmation Number:".$_REQUEST["txn_id"] ;
$msg .="\nDate: ".date("Y-m-d");
$msg .="\nAmount: ".$_REQUEST["mc_gross"];
$msg .="\nCampaign: ".$arrCustom[1];
$msg .="\nDonor: ".$_REQUEST["first_name"]." ".$_REQUEST["last_name"];

$from=get_option( 'from_address' );
$email_subject=get_option( 'email_subject' );
$send_mail_to_creator = $_REQUEST["campaign_creator_email"];
mail( $send_mail_to_creator , $email_subject, $msg, $from);
mail( $send_mail_to , $email_subject, $msg, $from);
mail( get_option( 'paypal_email_id' ) , $email_subject, $msg, $from);


}

Answers (2)

2011-06-20

Duncan O'Neill answers:

Hi Dimitry,

what error message ( if any ) or you getting when you try to send email? If none, try turning on WP_DEBUG, and see if that shows you any errors;


define('WP_DEBUG', true);


in /wp-config.php, in the root of your WP installation folder.

Aside from that, I can only guess that some variables you're relying on in the mail commands at the bottom of the code below are missing.

You could try echo-ing those variables out before you attempt to send the mail, to confirm whether or not that's the problem;


echo "send_mail_to creator is $send_mail_to_creator";
echo "send_mail_to is $send_mail_to";
echo "paypal email id is ".get_option('paypal_email_id');

mail( $send_mail_to_creator , $email_subject, $msg, $from);
mail( $send_mail_to , $email_subject, $msg, $from);
mail( get_option( 'paypal_email_id' ) , $email_subject, $msg, $from);


or put if statements in front of the mail commands to find out which one of the commands is failing.


if ($send_mail_to_creator) mail( $send_mail_to_creator , $email_subject, $msg, $from);
if ($send_mail_to) mail( $send_mail_to , $email_subject, $msg, $from);
if (get_option( 'paypal_email_id' )) mail( get_option( 'paypal_email_id' ) , $email_subject, $msg, $from);


hope this helps,

Duncan





Dmitriy Son comments:

Hi, Duncan!
This is the whole code.
I don't think that anything below the function is missing.
My mistake somewhere inside. The function doesn't work, i.e. it doesn't send messages to any of the addresses.
I think, items marked in bold are potentially wrong.
<?php

require("../../../wp-config.php");

global $wpdb;

if( class_exists('MyWpContribute') ) $objContribute = new MyWpContribute();


# Send notifications to here
$send_mail_to = get_option( 'paypal_email_id' );

# Your primary PayPal e-mail address
$msg = get_option( 'paypal_email_msg' );

#the emails will be coming from
$from = get_option( 'from_address' );

// Set the request paramaeter
$req = 'cmd=_notify-validate';

// Run through the posted array
foreach ($_REQUEST as $key => $value)
{
// If magic quotes is enabled strip slashes
if (get_magic_quotes_gpc())
{
$_REQUEST[$key] = stripslashes($value);
$value = stripslashes($value);
}
$value = urlencode($value);
// Add the value to the request parameter
$req .= "&$key=$value";
}

$url = "https://www.paypal.com/cgi-bin/webscr";

$ch = curl_init(); // Starts the curl handler
curl_setopt($ch, CURLOPT_URL,$url); // Sets the paypal address for curl
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Returns result to a variable instead of echoing
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // Sets a time limit for curl in seconds (do not set too low)
curl_setopt($ch, CURLOPT_POST, 1); // Set curl to send data using post
curl_setopt($ch, CURLOPT_POSTFIELDS, $req); // Add the request parameters to the post
$result = curl_exec($ch); // run the curl process (and return the result to $result
curl_close($ch);

if (strcmp ($result, "VERIFIED") == 0) // It may seem strange but this function returns 0 if the result matches the string So you MUST check it is 0 and not just do strcmp ($result, "VERIFIED") (the if will fail as it will equate the result as false)
{


foreach ($_REQUEST as $key => $value)
{

$msg.=$key."=>".$value.'<br>';
}


global $wpdb;

$table_name= $wpdb->prefix.'contribution';
[strong]$data = $wpdb->get_results("SELECT * FROM $table_name");
Inner Join wp_campaigns ON wp_contribution.cont_camp_id = wp_campaigns.camp_id
WHERE
wp_campaigns.camp_field_anonymous <> '1'";[/strong]

$arrCustom=split("#",$_REQUEST["custom"]);

$data["cont_camp_id"]=$arrCustom[0];

$data["cont_amt"]=$_REQUEST["mc_gross"];

if($_REQUEST["first_name"] != "" ) {
$data["cont_first_name"]= $wpdb->escape($_REQUEST["first_name"]);
}


if($_REQUEST["last_name"] != "" ) {
$data["cont_last_name"]= $wpdb->escape($_REQUEST["last_name"]) ;
}

if($_REQUEST["payer_email"] != "" ) {
$data["cont_email"]= $_REQUEST["payer_email"] ;
}

if($_REQUEST["campaign_creator_email"] != "" ) {
[strong]$data["camp_email"]= $wpdb->escape($_REQUEST["creator_email"]);[/strong]
}


$data["txn_id"]= $_REQUEST["txn_id"] ;

$data["cont_date"]= date("Y-m-d");

$rows_affected = $wpdb->insert( $table_name, $data );

$msg = get_option( 'paypal_email_msg' );

$from=get_option( 'from_address' );

$email_subject=get_option( 'email_subject' );

mail( $send_mail_to , $email_subject, $msg, $from);

// mail to donor, site admin and campaign creator

if($_REQUEST["payer_email"] != "" ) {
$msg .="\n\n==== Donation Information ====";
$msg .="\nConfirmation Number:".$_REQUEST["txn_id"] ;
$msg .="\nDate: ".date("Y-m-d");
$msg .="\nAmount: ".$_REQUEST["mc_gross"];
$msg .="\nCampaign: ".$arrCustom[1];
$msg .="\nDonor: ".$_REQUEST["first_name"]." ".$_REQUEST["last_name"];

$from=get_option( 'from_address' );
$email_subject=get_option( 'email_subject' );
$send_mail_to_creator = $_REQUEST["campaign_creator_email"];
mail( $send_mail_to_creator , $email_subject, $msg, $from);
mail( $send_mail_to , $email_subject, $msg, $from);
mail( get_option( 'paypal_email_id' ) , $email_subject, $msg, $from);


}

$eLog="/tmp/mailError.log";
//Get the size of the error log
//ensure it exists, create it if it doesn't
$fh= fopen($eLog, "a+");
fclose($fh);
$originalsize = filesize($eLog);

clearstatcache();
$finalsize = filesize($eLog);

}
else
{

header("Location: index.php");
die();
// Log an invalid request to look into
}

?>


Thanks,
Dmitriy.


Dmitriy Son comments:

There are potentially wrong items:

$table_name= $wpdb->prefix.'contribution';
$data = $wpdb->get_results("SELECT * FROM $table_name");
Inner Join wp_campaigns ON wp_contribution.cont_camp_id = wp_campaigns.camp_id
WHERE
wp_campaigns.camp_field_anonymous <> '1'";


if($_REQUEST["campaign_creator_email"] != "" ) {
$data["camp_email"]= $wpdb->escape($_REQUEST["creator_email"]);
}


Duncan O'Neill comments:

Dmitry,

Nothing in the above code is marked as bold.

Did you try any of my suggestions to try to pinpoint the error?

best,


Duncan O'Neill comments:

Dmitry,

I'm not a MySQL expert, but that inner join statement looks wonky to me. [[LINK href="http://dev.mysql.com/doc/refman/5.0/en/join.html"]]ref[[/LINK]]

Other possibilities;

If you've switched on WP_DEBUG ( see above ), that might give you the line which is creating the error.

You could also paste this php at the start of your file to tell you where your error lies;


ini_set('display_errors', 1);
error_reporting(E_ALL);


You could also try my other two original suggestions. :-)

best,

2011-06-21

fuscata answers:

Try:
$data = $wpdb->get_results("SELECT * FROM $table_name
Inner Join wp_campaigns ON wp_contribution.cont_camp_id = wp_campaigns.camp_id
WHERE
wp_campaigns.camp_field_anonymous <> '1'");


Dmitriy Son comments:

$table_name= $wpdb->prefix.'contribution';
$data = $wpdb->get_results("SELECT * FROM $table_name
Inner Join wp_campaigns ON wp_contribution.cont_camp_id = wp_campaigns.camp_id
WHERE
wp_campaigns.camp_email NOT NULL");


What's happenning with camp_email when $rows_affected = $wpdb->insert( $table_name, $data ) run?

Is it insert into wp_contribution as cont_first_name and so on?

I don't want to insert it into the table, I just want to get this field from wp_campaigns, send email and forget about it :)