Jump to content

Recommended Posts

I want to send people in my email database information that I have in there.  Basically I am sending players in a hockey league player info through an email.  I figured the easiest way to do this is by emailing them a table with the results of my database query in it.  When I tried to do this using phpmailer I got very lost.  Everything is fine except I'm not sure how to structure the code inside the $body variable so that it will show up in the email.  Here is the code I am using,

<?php
require("class.phpmailer.php");
require("class.smtp.php");
require("phpmailer.lang-en.php");

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->Host = "localhost"; // SMTP server
$mail->SMTPAuth = "true"; // turn on SMTP authentication
$mail->Username = "nick@barriemenshockey.com"; // SMTP username
$mail->Password = "******"; // SMTP password*/

include 'mailconfig.php';

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$mail->From = "nick@barriemenshockey.com";
$mail->FromName = "Barrie Mens Hockey League";

// HTML body
$body  = "Hey guys, <p>";
$body .= "Sorry about the delay in getting you your rosters but here they are, <br>";
$body .= 
$query1 = "SELECT name, teamname, jerseynumber, email, phonenumber FROM playerstats WHERE teamname = 'Boomtown Blades' ORDER BY jerseynumber ";
$result1 = mysql_query($query1);
$rows =mysql_fetch_assoc($result1);


//set up the table:
print("<table border=4 bordercolor = #184184 cellspacing=0 width=95% align=center><CAPTION><font size=4>Barrie Men's Hockey Roster 2007-08</font></CAPTION><tr>");

//print the proper column names in table headers:
echo "\n<tr>".
 "\n\t<th width=25><font size=3>Name</font></th>".
 "\n\t<th width=30><font size=3>Team</font></th>".
 "\n\t<th width=30><font size=3>#</font></th>".
 "\n\t<th width=60><font size=3>Email</font></th>".
 "\n\t<th width=30><font size=3>Phone #</font></th>".
 "\n</tr>"; 

 //loop through the table, printing
//the field values in table cells:
do
{
	print("<tr>");
	foreach($rows as $key=>$value)
	{
		print("<td><center>$value</center></td>");
	}
	print("</tr>");
}
while ($rows = mysql_fetch_assoc($result1));

//close out the table:
print("</tr></table>");

$body .= "Sincerely, <p>";
$body .= "Nick and Rob";

//non-HTML body
$textbody  = "Hey " . $row['name'] . ", \n\n";
$textbody .= "Check for hmtl first"
$textbody .= "Sincerely, \n";
$textbody .= "Nick and Rob";


$mail-> Subject = "BMHL Rosters";
$mail-> Body = $body;
$mail -> AltBody = $textbody;
$mail->AddAddress("nick@barriemenshockey.com");
$mail-> WordWrap = 50;

if(!$mail->Send())
{
   echo "There has been a mail error sending to " . $row["email"] . "<br>";

   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Message has been sent.';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();


?> 

 

I am using my email address to test it if you are wondering.  Thanks for any help.

 

Nick

Link to comment
https://forums.phpfreaks.com/topic/75713-code-inside-phpmailer/
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.