Jump to content

Code inside phpmailer


ninedoors

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 = "[email protected]"; // 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 = "[email protected]";
$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("[email protected]");
$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

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.