Jump to content

[SOLVED] how to mail the contents of an array.


BhA

Recommended Posts

Hi guys I was wondering whether is there a way to mail the contents of an array in php.

 

I tried to do smtg like down below

 

                 $resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
    $all = mysql_fetch_array($resultMissing))
    $content = array_values($all);

        $to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions.

			   
				$content
							  
							";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";[code]

But all I get to my email is:

"You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions.						   					1                                             "


I dont even understand why it gives me 1. While db table contains the following info.

      jane doe 3 
      john doe 2


I would appreciate the help thanks.

[/code]

Link to comment
Share on other sites

First of all this is bad syntax:

 

$message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions. $content";

 

It should be: $message = "You are missing the resumes..." . $content;

 

It's not wrong, but it can cause problems to mix strings and variables like you do.

 

Second, $syntax will hold an array of all the values. If you "echo $content" it will write "Array". So you have to somehow loop through the array and add the entries to your $message:

 

$message = "You are missing...."

$i = 0;
while($i < count($all)) {
  $message .= $all[$i];
}

 

Link to comment
Share on other sites

Thanks for your quick reply Wuhtzu but honestly I couldn't figured it out how to do that also when I tried to add a loop like you posted it didn't even send the mail. It is kind a frusturating I know it is quite simple but I dont understand why it quits sending the mail whenever I tried to add a loop into that.

 

                $resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
   $all = mysql_fetch_array($resultMissing);
                $ctr = mysql_num_rows($resultMissing);

$i = 0;
             while($i < $ctr) 
	 {
                             $message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions. $all[$i]";
                           }

        $to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";



?>

This is what I have now and I don't even get the mail sent message anymore.

Link to comment
Share on other sites

When you are using a function which returns true or false upon completion (true on success, false on error) you should always use that to check whether or not the function completed it's task successfully or not:

 

 

<?PHP

$sendmail = mail($to,$subject,$message,$headers);

if($sendmail) {
  echo "Mail sent successfully";
}
else {
  echo "Mail not send..";
}

?>

 

Please do that and determine if it's the mail() function which fails or if it's your mailbox which fails for some reason (eg. your $message becomes weird and it wont accept it or so)

 

 

Link to comment
Share on other sites

And I would still recommend you to do this:

 

$message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions " . $all[$i];

 

instead of

 

$message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions. $all[$i]";

 

 

Sooner or later you will get into trouble when mixing strings and variables like that :)

Link to comment
Share on other sites

Wuhtzu first of thanks for your help but its still not wrking I'll be posting the whole code maybe I am missing something.

 

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php

$db = mysql_connect("dbloc","user","pass");

    if ( $db != FALSE)
{
	echo "Connected to the database \n
	 Searching through the user profiles for non existing resumes... \n\n";
}
else
{
	echo "Connection failed to the database.";
}


mysql_select_db("dbname", $db);

$result = mysql_query("SELECT id, name, lname, resume FROM `test`", $db);


while ($row = mysql_fetch_array($result))
{

	$filename = $row["resume"];
	$id = $row["id"];		
	$name = $row["name"];
	$lname = $row["lname"];
	$link = "$filename";
	$table = "<table width=\"700\" align=\"center\" border=\"0\" cellspacing=\"0\" >";
        $tr = "<tr align=left bgcolor=#c0c0c0>";
        $td = "<td align=\"center\" >";
	$tds = "<td align=\"center\" width=\"350\">";



	if (file_exists("$link")) 
	{	
/*		
    			echo "$table \n $tr \n 
							   $td \n The resume for : </td>
							   $td \n $name $lname Employee ID: $id </td></tr>
						    $tr \n 
							   $tds \n exists. </td>
							   $td \n </td></tr>
							$tr \n 
							   $td \n </td>
							   $td \n </td></tr></table> ";


    */				
	} 
	else 
	{
/*			
	    	echo "$table \n $tr \n 
			                   $td \n The resume for : </td>
							   $td \n $name $lname Employee ID: $id </td> </tr>
							$tr \n
							   $tds \n does not exists. Please control the file name and location. </td>
							   $td \n </td></tr>
							 $tr \n 
							   $td \n </td>
							   $td \n </td></tr></table>";
*/			


        mysql_select_db("dbname", $db);
        $sql= "INSERT INTO test1 (name, lname, id) VALUES ('$name', '$lname', '$id')";

	if (!mysql_query($sql,$db))
        {
             die('Error: ' . mysql_error());
        }
              echo "1 record added";


	}
      }
  clearstatcache();


    $resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
    $all = mysql_fetch_array($resultMissing);
	$ctr = mysql_num_rows($resultMissing);

	$i = 0;
         while($i < $ctr) 
	 {
         $message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions " . $all[$i];
         }

        $to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        
       

        $sendmail = mail($to,$subject,$message,$headers);

        if($sendmail) {
        echo "Mail sent successfully";
        }
        else {
         echo "Mail not send..";
         }



?>

</body>
</html>

 

Well it seems like the code doesnt process the rest of the code after inserting the new values to the new table for people with missing resumes. When I put the mail function inside first while loop it was sendinmg me a seperate mail for each person missing a resume. I just wanted to receive only one mail containing all the information but I messed it up real bad I guess :P Anyway I apreciate the help and suggestions.

Link to comment
Share on other sites

First of all this is bad syntax:

 

$message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions. $content";

 

It should be: $message = "You are missing the resumes..." . $content;

 

Totally disagree. That is the whole reason the ability to parse variables was included with double quotes and the heredoc method. It is certainly a lot easy to code, debug and fix code that looks like this:

 

echo "Hello $firstname $lastname, your $year $make $model is due for a $service.";

 

than this:

 

echo "Hello ".$firstname." ".$lastname.", your ".$year." ".$make." ".$model." is due for a ".$service.".";

 

And there is no perceptable (sp?) difference between using variables within double quotes and using sigle quotes with the variables not parsed within the quotes: http://spindrop.us/2007/03/03/php-double-versus-single-quotes/

 

Link to comment
Share on other sites

Thanks for your input/thoughts/opinion mjdamato :) I simple passed on the advice I've always been given - to use "'some text ' . $var;" instead of "'some text $var';" and it has never given me trouble to do so...

 

But it might be worth looking into the use of double vs. single quotes.. not only for the performance perspective....

 

 

BhA... your script fails because your while-loop is infinite - $i has the value of 0 all the time and therefore it remains ($i < $ctr) remains true for ever :) You have to add 1 to $i for every loop:

 

$i = 0;
while($i < $ctr) {
  $message = "You are missing....";
  $i++;
}

 

It's kind of my fault, I forgot the $i++; part in the code I posted earlier.

Link to comment
Share on other sites

It should display one entry, the last one, since you keep overwriting $message for each loop..

 

You need to do this:

 


$i = 0;
while($i < $ctr) {
  $message .= "You are missing...." . $all[$i];
  $i++;
}

 

Notice the .=

 

 

Link to comment
Share on other sites

<?php
$resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
while ($all = mysql_fetch_array($resultMissing)) {
    $content .= array_values($all); // note .=
}

        $to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $message = "You are missing the resumes on the following profiles, Please control the file name and location or take necessary actions.\n  $content \n";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";
?>

 

Give that a try.

 

You were not fetching all the data from MySQL, which is why you would only get one result. Now you are and $content is being appended to house the data.

Link to comment
Share on other sites

Wuhtzu and Frost110 thanks for your replies I tried both of changes you advised but unfortunatly it didn't worked as I planned.

 

After applying the changes wuhtzu advised I got :

 

"You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions. 2 john doe"

 

2 john doe is my first row in the array when it comes to the rest. it just doesnt post them.

 

 

And after applying the changes frost advised I got :

 

"You are missing the resumes on the following profiles, Please control the file name and location or take necessary actions. ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray"

 

And frost's changes allow me the fetch right amount of rows but then It just displays them as Array instead of showing the content.

 

 

And I still couldn/t figure out where I am making the mistake. Maybe I shall try to convert the array into excel file ana try to mail that file do you guys think will it work that way?

 

latest form of the code is as follows:

 

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php

$db = mysql_connect("dbloc","user","pass");

    if ( $db != FALSE)
{
	echo "Connected to the database \n
	 Searching through the user profiles for non existing resumes... \n\n";
}
else
{
	echo "Connection failed to the database.";
}


mysql_select_db("dbname", $db);

$result = mysql_query("SELECT id, name, lname, resume FROM `test`", $db);


while ($row = mysql_fetch_array($result))
{

	$filename = $row["resume"];
	$id = $row["id"];		
	$name = $row["name"];
	$lname = $row["lname"];
	$link = "$filename";


	if (file_exists("$link")) 
	{	

	} 
	else 
	{
	   mysql_select_db("dbname", $db);
           $sql= "INSERT INTO test1 (name, lname, id) VALUES ('$name', '$lname', '$id')";
	  
	   if (!mysql_query($sql,$db))
           {
               die('Error: ' . mysql_error());
           }
              echo "1 record added";
	   }
	  
      }
  clearstatcache();


//	    $resultMissing = mysql_query("SELECT id, name, lname FROM `test1`");
//	    $all = mysql_fetch_array($resultMissing);


	$resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
        $ctr = mysql_num_rows($resultMissing);

	while ($all = mysql_fetch_array($resultMissing))
	 {
          
                 $content .= array_values($all);  
                
		  // note .=
         }      


        
	$to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $message = "You are missing the resumes on the following profiles, Please control the file name and location or take necessary actions.\n  $content \n";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";



/*		


        $to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $from = "akinalpburak@hotmail.com";
	$message = "You are missing the resumes on the following profiles,Please control the file name and location or take necessary actions \n" .$messageContent;
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";


$sqldel= "TRUNCATE TABLE test1";
    if (!mysql_query($sqldel,$db))
        {
             die('Error: ' . mysql_error());
        }
              echo "records deleted";
*/		
?>

</body>
</html>

Link to comment
Share on other sites

 

I read your code it didnt make sence.

 

So try this and see if it emails you with the users info in test1 ok.

 

<html>

<head>

<title>test email from redarrow</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<?php

$db = mysql_connect("dbloc","user","pass");
mysql_select_db("dbname", $db)or die("mysql_error()");
      
$query = "SELECT `id`, `name`, `lname`, `resume` FROM `test`";
$result=mysql_query($result);

while ($row = mysql_fetch_assoc($result)){

$filename = $row["resume"];
$id = $row["id"];		
$name = $row["name"];
$lname = $row["lname"];
$link = "$filename";
       
if (file_exists($link)) {	

} else {
	   
$name=addslashes($_POST['name']);
$lname=addslashes($_POST['name']);	
$id=addslashes($_POST['id']);
           
$query2= "INSERT INTO test1 (name, lname, id) VALUES ('$name', '$lname', '$id')";
$result2=mysql_query($query2) or die("mysql_error()");
	  
$query3 ="SELECT `id`, `name`, `lname` FROM `test1`";

$result3=mysql_query($query3);

while ($fetch_email_info = mysql_fetch_array($resultMissing))
	 {
$to = "akinalpburak@hotmail.com";
        
$subject = "missing resumes";
        
$message = " user id: $fetch_email_info ['id'] <br> name:  $fetch_email_info['$name']  <br> surname: $fetch_email_info['lname'] ";
        
$from = "akinalpburak@hotmail.com";
        
$headers = "From: $from";
        
if(mail($to,$subject,$message,$headers)){
        	
echo " mail was sent!";

}else{

echo " mail not sent!";

}
        
}

} 

?>
</body>
</html>

Link to comment
Share on other sites

<?php
$resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
while ($all = mysql_fetch_array($resultMissing)) {
    $content .= implode(" : ", $all) . "\n"; // note .=
}

        $to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $message = "You are missing the resumes on the following profiles, Please control the file name and location or take necessary actions.\n  $content \n";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";
?>

 

www.php.net/implode  sorry about that wrong function in there.

Link to comment
Share on other sites

Thank you all it works perfectly now :P

 

code is as follows just in case someone may need smtg like this.

 

 

 

 <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php

$db = mysql_connect("dbloc","user","pass");

    if ( $db != FALSE)
{
	echo "Connected to the database \n
	 Searching through the user profiles for non existing resumes... \n\n";
}
else
{
	echo "Connection failed to the database.";
}


mysql_select_db("dbname", $db);

$result = mysql_query("SELECT id, name, lname, resume FROM `test`", $db);


while ($row = mysql_fetch_array($result))
{

	$filename = $row["resume"];
	$id = $row["id"];		
	$name = $row["name"];
	$lname = $row["lname"];
	$link = "$filename";


	if (file_exists("$link")) 
	{	

	} 
	else 
	{
	   mysql_select_db("dbname", $db);
           $sql= "INSERT INTO test1 (name, lname, id) VALUES ('$name', '$lname', '$id')";
	  
	   if (!mysql_query($sql,$db))
           {
               die('Error: ' . mysql_error());
           }
              echo "1 record added";
	   }
	  
      }
  clearstatcache();



        $resultMissing = mysql_query("SELECT id, name, lname FROM `test1`", $db);
        while ($all = mysql_fetch_array($resultMissing)) 
	{
          $content .= implode(" : ", $all) . "\n";
    }
	    

	 $i = 0;
         while($i < $ctr) 
	 {
         $messageContent .= " ". $all[$i];
         $i++;
         }
        
	$to = "akinalpburak@hotmail.com";
        $subject = "missing resumes";
        $message = "You are missing the resumes on the following profiles, Please control the file name and location or take necessary actions.\n  $content \n";
        $from = "akinalpburak@hotmail.com";
        $headers = "From: $from";
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";





$sqldel= "TRUNCATE TABLE test1";
    if (!mysql_query($sqldel,$db))
        {
             die('Error: ' . mysql_error());
        }
              echo "records deleted";

?>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.