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@user.com"; 
    $from = "email@user.com ( 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: whoever@whereever.com"; 
    //$headers .= "Bcc: whoever@whereever.com";
     
    // 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
Share on other sites

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.

Link to comment
Share on other sites

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..??

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

----  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?
*/

?>

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.