totalityventures Posted August 4, 2010 Share Posted August 4, 2010 Hi there hope someone can help. (i'm new to php and sql) I've put together a couple of queries that builds a table. This table is to go into an email to each user on a database. I've tested this by outputting the results to a website and it all works fine - it displays the email as I want it to so all I need to do now is set it up so that it can be emailed. I am struggling with how to build the body string - I can do every bit until the table is built. I basically have an SQL query that I use for the start of the body text (creates $body1, $body2, $body3 (headings of table). and then the following code below and then $body5... etc. for the end of the body. I then just want to join them together for the email. (which I can do) But how do I get the whole of the table below into a string ($body4) - can I just put something around it all? Using $bodyX just returns the last row as you would expect. Please help Thanks in advance while($row2 = mysql_fetch_array($sql2)){ if ((strtolower($row2["ad_town"]) == strtolower($targettown) OR strtolower($row2["ad_town"]) == strtolower($target2town) OR strtolower($row2["ad_town"]) == strtolower($target3town)) AND (strtolower($row2["ad_townseek"]) == strtolower($mytown) OR strtolower($row2["ad_alttownseek"]) == strtolower($mytown) OR strtolower($row2["ad_altalttownseek"]) == strtolower($mytown))) { $matchtype = 'Exact'; } ELSE { $matchtype = 'County'; } $adverttown = $row2["ad_town"]; $adheadline = $row2["ad_headline"]; $nobeds = $row2["ad_nobeds"]; $rent = $row2["ad_rent"]; $adid = $row2["13"]; $bodyX = ' <tr><td style="width: 10%;"> ' . $matchtype . ' <td style="width: 20%;"> ' . $adverttown . ' </td><td style="width: 55%;"> ' . $adheadline . ' </td><td style="width: 5%; text-align: center;"> ' . $nobeds . ' </td><td style="width: 5%; text-align: center;">£ ' . $rent . ' </td><td style="width: 5%;text-align: center;"><a href="http://ipropertyswap.co.uk/index.php/home-swap-adverts?page=show_ad&adid= ' . $adid . ' ">view</a></td></tr> '; echo $bodyX; } Quote Link to comment Share on other sites More sharing options...
Skewled Posted August 4, 2010 Share Posted August 4, 2010 http://php.net/manual/en/function.mail.php That will show you how the mail function works, then all you have to do is incorporate it into your code. Quote Link to comment Share on other sites More sharing options...
totalityventures Posted August 4, 2010 Author Share Posted August 4, 2010 I understand the mail function I don't undertand how to get my code into a string to use the mail function. Quote Link to comment Share on other sites More sharing options...
Skewled Posted August 4, 2010 Share Posted August 4, 2010 I see.. // build an array for your email users $email[] = ('user1', 'user2', 'user3'); // Do a foreach loop to make sure each user receives an email foreach.... { $to = $email; // Who you want the e-mail sent to $subject = 'Table Output'; // The e-mail subject $bodyX = ' <tr><td style="width: 10%;"> ' . $matchtype . ' <td style="width: 20%;"> ' . $adverttown . ' </td><td style="width: 55%;"> ' . $adheadline . ' </td><td style="width: 5%; text-align: center;"> ' . $nobeds . ' </td><td style="width: 5%; text-align: center;">£ ' . $rent . ' </td><td style="width: 5%;text-align: center;"><a href="http://ipropertyswap.co.uk/index.php/home-swap-adverts?page=show_ad&adid= ' . $adid . ' ">view</a></td></tr> '; $headers = 'FROM:youremail@yourdomain.com' . "\r\n"; // Setting the Header here mail($to, $subject, $bodyX, $headers); // Send the e-mail } // End foreach Quote Link to comment Share on other sites More sharing options...
radar Posted August 4, 2010 Share Posted August 4, 2010 in your example, why not use $bodyX .= instead of just =, as that will append all elements together. then you can take that echo bodyx out, and use $bodyX for your body of the email. On a side note, use your mail function similar to this: $header .= "Reply-To: Some One <someone@mydomain.com>\r\n"; $header .= "Return-Path: Some One <someone@mydomain.com>\r\n"; $header .= "From: Some One <mydomain@myhost.com>\r\n"; $header .= "Organization: My Organization\r\n"; $header .= "Content-Type: text/html\r\n"; mail("recipient@recipient-domain.com", "Test Message", "This is my message.", $header); doing it t his way, you will beat most spam filters. Quote Link to comment Share on other sites More sharing options...
Skewled Posted August 4, 2010 Share Posted August 4, 2010 Your correct Radar, I modified the code above to be a bit more clearer, I hope the rest of my code is correct. Quote Link to comment Share on other sites More sharing options...
totalityventures Posted August 26, 2010 Author Share Posted August 26, 2010 Sorry for the delay in my response but I was on holiday. Thanks for your help guys but I am unfortunately still confused as to how to implement this: There are several loops in my php and this is one embedded in it - I can't put the mailto part within this loop: This part builds a table that forms part of an email - so it could create zero rows up to 100 rows - surely if I put the mailto within this query it would mail the same person several times - once for each row. I was hoping that I could put something around the text I posted along the lines of $bodyY= [and then the posted part] so that I would call this text within another loop when I get to a point where the whole of the body of the email has been created. Really appreciate your help. Quote Link to comment Share on other sites More sharing options...
totalityventures Posted September 5, 2010 Author Share Posted September 5, 2010 Anyone? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.