This topic is locked
[SOLVED]

  Email Multiple Recipients - Query Based

11/5/2012 1:14:19 PM
PHPRunner General questions
D
DAndras author

Hello,
I am currently attempting to utilize the PHPRunner functionality of mailing recipients when an add/edit has occurred on a page; however, I am running in to some issues when I attempt to make that mailing list dynamically based on a query.
I am currently running PHPRunner 6.1.
I have run into quite a few problems, thus far, a few of which I have solved.
First, the script would inform me that a 'database has not been selected', which I fixed.

Then, PHPRunner informed me that the email function does not recognize arrays, so I used implode to create a comma-separated string of the array.
After playing around with the event a bit, I came up with the following:

global $conn;

$conn = mysql_connect("server","name","pass");

if (!$conn)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("dbname",$conn);

$query = "SELECT email FROM MailMember";

$result = mysql_query($query) or die(mysql_error());

while($maillist = mysql_fetch_array($result))

$mailstring = implode(",", $maillist);

//********** Send email with new data ************
$email="$mailstring";

$from="mailaddress";

$msg="";

$subject="New IOS Model/Device Added";
global $pageObject;
foreach($values as $field=>$value)

{

if(!IsBinaryType($pageObject->pSet->getFieldType($field)))

$msg.= $field." : ".$value."\r\n";

}



$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from));

if(!$ret["mailed"])

echo $ret["message"];


I have added two email address which should be returned as the result of the query. The email, however, is not getting to either of those addresses.
If I replace the variable recipient with a simple comma-separated list, it works; however, this would be tedious for me to update and rebuild the project constantly.
If you have any suggestions or notice any blatant errors I've over-looked, I would greatly appreciate your help! I happily accept all criticism as my knowledge of php is a work-in-progress.

admin 11/5/2012

Instead of

$email="$mailstring";

$from="mailaddress";


you need to use

$email=$mailstring;

$from=$mailaddress;
D
DAndras author 11/5/2012



Instead of

$email="$mailstring";

$from="mailaddress";


you need to use

$email=$mailstring;

$from=$mailaddress;



Hello,
I actually had that initially. The addition of the double quotations was something that I had just attempted on this most recent failed fix. The second part, "mailaddress" is actually our local administration address, so it is in double quotes. I simply replaced a lot of names/address/etc with generalities due to security issues.
Thanks for the help, though, and I'm open to any other suggestions!

admin 11/5/2012

As a general advice - you need to add a few echo statements to print variables on the screen. This way you can what parts of code are executed and what are the values of variables like $mailstring.

D
DAndras author 11/6/2012



As a general advice - you need to add a few echo statements to print variables on the screen. This way you can what parts of code are executed and what are the values of variables like $mailstring.


Hello,
I ran the following code which prints out the values that I am looking for; however, I can not discern how I should go about assigning this output to a variable/array that I can insert into the pre-made email function.
global $conn;

$conn = mysql_connect("server","name","pass");

if (!$conn)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("NetManage",$conn);

$query = "SELECT email FROM MailTest";
echo "\$query = $query \r\n";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

echo $row['email'];

echo "<br />";

}
This outputs:

email@example.com

test@test.com

test2@test.com
I tried a few solutions to no avail.
Any suggestions?

admin 11/6/2012

Where you print the current email address on the page ($row['email']) use the following to accumulate a list of email addresses in the variable:

$email.=$row['email'].",";


Once you processed all records $email variable contains the list of comma separate email addresses.

D
DAndras author 11/6/2012



Where you print the current email address on the page ($row['email']) use the following to accumulate a list of email addresses in the variable:

$email.=$row['email'].",";


Once you processed all records $email variable contains the list of comma separate email addresses.



You, sir, are a boon of peace and great stress reliever in my life.
Thanks!