Page 1 of 1

sending simplexml values in an email

Posted: Mon Feb 07, 2011 7:48 am
by chandrika
I am trying to send the titles in an xml file to mysefl in an email, but only seem to be able to get the email to print 0 or 1,

I can echo the titles in the xml using the following code :

Code: Select all

if( ! $xml = simplexml_load_file('/path/to/my/file.xml') )
    {
        echo 'unable to load XML file';
    }
else
   {
foreach( $xml as $item )
        { 
//returns all the <title> from the xml file
           $lc_title = $item->title;
           echo $lc_title.'<br />';
         }
    }
This will print on screen
Title One
Title Two
TitleThree etc...

So I can extract the titles in this way, but am not sure how to send those results in an email.

If I use mail() within the foreach statement and put in the message the variable $lc_title like this

Code: Select all

$to = "me@mymail.com";
$subject = "book titles";
$message = "The titles of the books are <br />".$lc_title."<br />";
$from = "server@mysite.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
I get an email for each title, but I want just one email with all the titles. Putting it outside just returns one value, not all of them.

I am not sure how to pass all the values in that foreach statement, out of it as a string to be used in my email.

Re: sending simplexml values in an email

Posted: Mon Feb 07, 2011 8:54 am
by anantha
first you have to store all the tittle in an array like this

Code: Select all

$lc_title = array();
foreach( $xml as $item )
        { 
//returns all the <title> from the xml file
           $lc_title[] = $item->title;
           echo $lc_title.'<br />';
         }
and in the email

Code: Select all


$message = "The titles of the books are <br />";
foreach($lc_title as $title)
{
	$message .= $title;
	$message .= "<br />";
}


Re: sending simplexml values in an email

Posted: Mon Feb 07, 2011 10:11 am
by chandrika
Thankyou, that works perfectly. I had tried something similar, but kept getting errors with the way I was trying to do it.

Very much appreciated!