Jump to content

while (during e-mail message)


PHPNewbie55

Recommended Posts

OK..

I am trying to create a newsletter script that sends products from my database to users e-mail addy...

 

Here is my problem::::

 


    $to = "[email protected]"; 
    $from = "[email protected] ( My Newsletter )"; 
    $subject = "Weekly Newsletter - $date";



$message = " HOW DO I GET A WHILE STATEMENT TO RUN HERE ";



    $headers  = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 

    //options to send to cc+bcc 
    //$headers .= "Cc: [email protected]"; 
    //$headers .= "Bcc: [email protected]";
     
    // send the email. 
    mail($to, $subject, $message, $headers);




 

 

I am trying to send multiple products from multiple categories but I can't figure out how to get a WHILE statement to run for the MEASSAGE portion of the e-mail...

 

 

Any help would be appreciated....

Thanks In Advance!!

Link to comment
https://forums.phpfreaks.com/topic/81740-while-during-e-mail-message/
Share on other sites

First decide what stays the same (header? to email? subject string? message?) then define these variables.

 

Next define a query to pull all the records from your database.

Now run that query in a WHILE loop sending the email to everyone in your database.

I would probably break it down into a function like:

function SendTheEmail($username, $fname, $lname, $anyOtherInfoYouMightWant) {
     //do something here
}

 

Then just pull stuff from the database:

 

//pretend mysql link is established
$q = mysql_query("SELECT * FROM users");
while($r = mysql_fetch_assoc($q)) {
     SendTheEmail($r['username'], $r['fname'], $r['lname'], $r['somethingelse']);
}

 

SendTheEmail would contain the email text, and the variables would be plugged in.... Or, if you wanted a reusable function, you could make it where you also sent the text to the function.

I ran into a problem...

 

The problem is that the email addresses and the products are in two different tables...

 

So I can run a WHILE loop for the emails...  but I can't figure out how to run another WHILE loop to include the products in each email...

 

Anyone have any ideas.....??

;D wooo hooo...

 

I got it....  It works....

 

I'll post the code as soon as I get the bugs worked out...  But I did get multiple products in one e-mail...

 

The problem was I was combining the WHILE LOOPS and I didn't have to......

 

Back in a bit....

OK Here is my code.....

Now I am a beginner so if anyone sees anything weird in my code OR

see anything that doesn't need to be there.. OR a better way to do this...  (PLEASE LET ME KNOW)

 

<?PHP 

//////  MAKE A CONNECTION
$mysqlserver = "localhost";
$mysqllogin =  "login_name";
$mysqlpassword = "login_password";
$mysqldb = "database_name"; 
$conn  = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword");
// IF CONNECTION CANNOT BE MADE QUIT AND GIVE AN ERROR MESSAGE
if (!$conn) { die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error()); }
mysql_select_db("$mysqldb", $conn);

//////////  query the products table

$product_query = mysql_query("SELECT * FROM products_table ORDER BY title desc limit 10");
while($info = mysql_fetch_array($product_query))  {

$message_body .= "THIS WILL BE YOUR HTML FOR EACH PRODUCT";

} // end the while($info = mysql_fetch_array($product_query)) loop	

#########################################
## Now that you have all of your products chosen start looping through the e-mail table...
#########################################

$query = mysql_query("SELECT * FROM my_newsletter_table WHERE confirmed='Yes'");
while($row = mysql_fetch_array($query)) {

/// to personalize each e-mail create a beginning and an ending for each...
$message_begin = "<html><body><p><b>Hello ".$row['name']."</b>,<br>This is our weekly newsletter that you requested..</p>";
$message_end = "</body></html>";


//  grab the e-mail address...
$email = $row['email'];
// create the date...
$date = date("m-d-y");

//  set up the outgoing e-mail...
    $to = "$email"; 
    $from = "$email"; 
    $subject = "My Newsletter for $date";
    $headers  = "From: $from ( My Brand New Newsletter )\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 

//  now set up the message to send combining the Begin - BODY - End
$message .= "$message_begin $message_body $message_end";

// now send it...	

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

} // end while($row = mysql_fetch_array($query))

// you are finished... messages sent....	

?>

 

Thanks..  and any tips would be appreciated

Anything that will help me learn MORE......

 

Teaching myself sucks... I have one book.. that I rarely open..  and the rest is trial and error...

and also a lot of looking around at other peoples codes to see how they put stuff together...

so it is a time consuming process..  but I am learning!!

 

----  One Question...  How do you get your code to be COLORED like it is in some posts..??

OOOOOps....

 

 

I just tested that code with multiple e-mail addresses......

 

It sends one e-mail for every address that is in the table..

The first e-mail is perfect..

The second e-mail contains both the first and second e-mail address information along with two sets of products...

The third e-mail contains all three sets of email info and three sets of products...

 

Weird........  since the send mail is inside a WHILE loop I thought it would send each individual e-mail with only one set of MESSAGE_BODY.....

Ah Ha.....

 

I found the error....

 

 

This part......

$message .= "$message_begin $message_body $message_end";

 

Should be......

$message = "$message_begin $message_body $message_end";

 

Without the period before the equals sign....

 

"I really don't even know what that indicates.. if someone would care to shed some light on that.. it would be cool..."

 

I have tested it several times now and it does work.....

 

So here is the code...

<?PHP 

//////  MAKE A CONNECTION
$mysqlserver = "localhost";
$mysqllogin =  "login_name";
$mysqlpassword = "login_password";
$mysqldb = "database_name"; 
$conn  = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword");
// IF CONNECTION CANNOT BE MADE QUIT AND GIVE AN ERROR MESSAGE
if (!$conn) { die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error()); }
mysql_select_db("$mysqldb", $conn);

//////////  query the products table

$product_query = mysql_query("SELECT * FROM products_table ORDER BY title desc limit 10");
while($info = mysql_fetch_array($product_query))  {

$message_body .= "THIS WILL BE YOUR HTML FOR EACH PRODUCT";

} // end the while($info = mysql_fetch_array($product_query)) loop	

#########################################
## Now that you have all of your products chosen start looping through the e-mail table...
#########################################

$query = mysql_query("SELECT * FROM my_newsletter_table WHERE confirmed='Yes'");
while($row = mysql_fetch_array($query)) {

/// to personalize each e-mail create a beginning and an ending for each...
$message_begin = "<html><body><p><b>Hello ".$row['name']."</b>,<br>This is our weekly newsletter that you requested..</p>";
$message_end = "</body></html>";


//  grab the e-mail address...
$email = $row['email'];
// create the date...
$date = date("m-d-y");

//  set up the outgoing e-mail...
   $to = "$email"; 
   $from = "$email"; 
   $subject = "My Newsletter for $date";
   $headers  = "From: $from ( My Brand New Newsletter )\r\n"; 
   $headers .= "Content-type: text/html\r\n"; 

//  now set up the message to send combining the Begin - BODY - End
$message = "$message_begin $message_body $message_end";

// now send it...	

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

} // end while($row = mysql_fetch_array($query))

// you are finished... messages sent....	

?>

----  One Question...  How do you get your code to be COLORED like it is in some posts..??

 

I found that when you use PHP instead of php it doesnt get colored,

example:

 

<?PHP

$not = 'This is a non-colored code on PHP FREAKS';

echo $not;

?>

 

<?php

$yes = 'This is a colored code on PHP FREAKS';

echo $yes;

/*
See the difference?
*/

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.