Newbie Question about mailing from array
Moderator: General Moderators
Newbie Question about mailing from array
Hello,
I am pulling in data from a database to mail the customers that sign up for my service. Here is the code I have written so far:
$result = mysql_query('SELECT email FROM customers');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {$query = "SELECT DISTINCT email FROM customers";
$rcount= @mysql_numrows($result);
$result= mysql_query($query) or suicide(__LINE__, __FILE__, mysql_error());
$row = mysql_fetch_row($result);
echo $rcount;
while ( $r = mysql_fetch_array($locationResult) ) {
echo $value;
$to = $value;
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}
}
Can someone help?
I am pulling in data from a database to mail the customers that sign up for my service. Here is the code I have written so far:
$result = mysql_query('SELECT email FROM customers');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {$query = "SELECT DISTINCT email FROM customers";
$rcount= @mysql_numrows($result);
$result= mysql_query($query) or suicide(__LINE__, __FILE__, mysql_error());
$row = mysql_fetch_row($result);
echo $rcount;
while ( $r = mysql_fetch_array($locationResult) ) {
echo $value;
$to = $value;
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}
}
Can someone help?
Re: Newbie Question about mailing from array
I'm not sure what you're asking here - is your script not working?
A few points though:
- mysql_numrows isn't a function unless you've created it elsewhere - you might be looking for mysql_num_rows.
- The @ symbol infront of the mysql_numrows function is supressing any errors/warnings it may be producing. This is not recommended, especially when debugging.
- The function 'suicide' isn't a core function, I'm assuming this is your custom function.
- "$locationResult" and "$value" are used but never set
- For something like this where you're probably going to be looping through a large list it's worthwhile using mysql_fetch_assoc over mysql_fetch_row
Your script is all a bit of a mishmash using undefined variables and never calling one's you've defined.
A few points though:
- mysql_numrows isn't a function unless you've created it elsewhere - you might be looking for mysql_num_rows.
- The @ symbol infront of the mysql_numrows function is supressing any errors/warnings it may be producing. This is not recommended, especially when debugging.
- The function 'suicide' isn't a core function, I'm assuming this is your custom function.
- "$locationResult" and "$value" are used but never set
- For something like this where you're probably going to be looping through a large list it's worthwhile using mysql_fetch_assoc over mysql_fetch_row
Your script is all a bit of a mishmash using undefined variables and never calling one's you've defined.
Re: Newbie Question about mailing from array
Thanks for the quick reply. I am aware of some of the things you said. I am not concerned or troubleshooting that part. When I try and plug $results into a mail function for $to, it tells me that it is the wrong data type. My goal is simply to have a script that pulls info from emails row of customers table and have it mail everyone on it.
The thing about this script that I cannot figure out is when I echo the $results, it only outputs the first line. So it is something simple and stupid that I am missing.
Much of this code is a collection of scripts that I have been troubleshooting with. My first one did not work so I started replacing parts.
Anyone have an suggestions...?
The thing about this script that I cannot figure out is when I echo the $results, it only outputs the first line. So it is something simple and stupid that I am missing.
Much of this code is a collection of scripts that I have been troubleshooting with. My first one did not work so I started replacing parts.
Anyone have an suggestions...?
Re: Newbie Question about mailing from array
Well $results doesn't exist - if you mean $result then that would be a resource, so of course mail() would not accept this.
I suggest some simple debugging like var_dump() any of the variables you think you have a problem with, this will tell you their values and their data types and should make it easier to see what's going on.
Something like this is actually quite simple so I don't think you need to go cutting random pieces of code and putting them together, just take a few minutes to write down some pseudo code and then code it yourself from scratch - that way you'll understand it more and it's a better way to learn.
I suggest some simple debugging like var_dump() any of the variables you think you have a problem with, this will tell you their values and their data types and should make it easier to see what's going on.
Something like this is actually quite simple so I don't think you need to go cutting random pieces of code and putting them together, just take a few minutes to write down some pseudo code and then code it yourself from scratch - that way you'll understand it more and it's a better way to learn.
Re: Newbie Question about mailing from array
Hey MikeMike,
Yes, I know it is simple. I just need a little guidance. The problem is when I am pulling it from the DB, I am building an array. The mail function will not accept arrays, so I have to convert it to a string. I have search the internet and gone through a few books I bought and I cannot find anything. Hence why I am here in the forums.
If you have any advice on how to look this up, that would be great.
I know for a fact I am using an array and I feel you are answering questions I am not asking. I am learning PHP at the best pace I can and rarely visit the forums for advice. When I do, I am looking for specific advice on how to do something. As you see, I am giving it a shot on my own. I am piecing it togehter in my mind to understand the way I learn.
With that said, I have the script working up to the point of sending it to multiple email addresses. It will send the email to the first address from the DB only. I have tried many things, but just not grasping it. I just need someone to push me in the right direction on the specific issue.
Thanks for your advice in advance.
Yes, I know it is simple. I just need a little guidance. The problem is when I am pulling it from the DB, I am building an array. The mail function will not accept arrays, so I have to convert it to a string. I have search the internet and gone through a few books I bought and I cannot find anything. Hence why I am here in the forums.
If you have any advice on how to look this up, that would be great.
I know for a fact I am using an array and I feel you are answering questions I am not asking. I am learning PHP at the best pace I can and rarely visit the forums for advice. When I do, I am looking for specific advice on how to do something. As you see, I am giving it a shot on my own. I am piecing it togehter in my mind to understand the way I learn.
With that said, I have the script working up to the point of sending it to multiple email addresses. It will send the email to the first address from the DB only. I have tried many things, but just not grasping it. I just need someone to push me in the right direction on the specific issue.
Thanks for your advice in advance.
Re: Newbie Question about mailing from array
By the way...this is the code I wrote my self. Without the use of any code snippets:
Code: Select all
<?php
require_once('includes/config.inc');
require_once('includes/dbc.inc');
$result = mysql_query('SELECT email FROM customers');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {
$query = "SELECT email FROM customers ";
$rcount= mysql_num_rows($result);
$result= mysql_query($query) or suicide(__LINE__, __FILE__, mysql_error());
$row = mysql_fetch_row($result);
foreach ($row as $value) {
$to = $value;
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}
}
vRe: Newbie Question about mailing from array
Ok, well if you know that you're passing an array of values to mail() then you have two options:
1. You could pass eveything to mail() in one string in a comma-seperated list. For example:
In order to do this from an array you'd need to implode your array - this seperates each entry by a delimiter of your choosing:
2. You could send a seperate mail() call for each entry in the array using foreach. For example
This would loop through and send an email to every email address in your array.
This is just a quick idea of where you need to go from here, but I can notice a few things wrong with your code just at quick glance.
1. You could pass eveything to mail() in one string in a comma-seperated list. For example:
Code: Select all
mail('me@example.com, you@example.com, us@example.com', $subject, $body);Code: Select all
$comma_seperated_list = implode(',', $your_array);Code: Select all
foreach($your_array as $email){
mail($email, $subject, $body);
}This is just a quick idea of where you need to go from here, but I can notice a few things wrong with your code just at quick glance.
Re: Newbie Question about mailing from array
thanks for the advice. Do you have any idea why it is just pulling the first emial from the row?
It should process two seperate emails that I have in there right now, but it is stoping with the first one!
I actually was exploring the implode feature, which an array is seperated buy a => right...so would that be my delimerter?
It should process two seperate emails that I have in there right now, but it is stoping with the first one!
I actually was exploring the implode feature, which an array is seperated buy a => right...so would that be my delimerter?
Re: Newbie Question about mailing from array
So I have cleaned it up a bit but I am still just getting 1 email from the query. The funny thing is if I echo the count, it shows two rows found...
When I run the Var Dump, it shows that there is only on value in the array, which makes me wonder if it is my query.
resource(5) of type (mysql result) array(1) { [0]=> string(17) "admin@test.com" } string(17) "admin@test.com"
Code: Select all
<?php
require_once('includes/config.inc');
require_once('includes/dbc.inc');
$result = mysql_query('SELECT email FROM customers');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {
$query = 'SELECT email FROM customers';
$result2= mysql_query($query)
or suicide(__LINE__, __FILE__, mysql_error());
$row = mysql_fetch_row($result2);
$subject = "Message from Me";
$body = "Hi,\n\nWe just wanted you to know.";
foreach ($row as $value) {
if (mail($value, $subject, $body)) {
echo("<p>Message successfully sent to ". $value ."</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}
}
?>
When I run the Var Dump, it shows that there is only on value in the array, which makes me wonder if it is my query.
resource(5) of type (mysql result) array(1) { [0]=> string(17) "admin@test.com" } string(17) "admin@test.com"
Re: Newbie Question about mailing from array
Got it... This is how you can take an array and put it into a string list seperated by a comma:
Code: Select all
<?php
require_once('includes/config.inc');
require_once('includes/dbc.inc');
//Define Subject
$subject = "Hello";
//Define Body
$body = "How are you";
//Pull Emials Addresses from DB
$result = mysql_query('SELECT email FROM customers');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {
$query = 'SELECT * FROM customers';
$result= mysql_query($query)
or suicide(__LINE__, __FILE__, mysql_error());
if ($row = mysql_fetch_array($result))
{
$to = $row["email"];
$ok = true;
}
while ($row = mysql_fetch_array($result))
{
$to .= ", " . $row["email"];
$ok = true;
}
//Mail It
mail($to, $subject, $body);
}
?>