Emailing an html page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Emailing an html page

Post by mccommunity »

I have a php page that does a query on a db and spits out a page of records. I would like there to be an email box on that page so they could enter and email and email it. I really have no idea how to do this. Has someone done this that could show me an example or share some code or at least just steer me in the right direction? Thanks you in advance for your help.

Mark
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

There is an example at http://www.php.net/manual/en/function.mail.php that shows how to email that has HTML.

Code: Select all

<?php
/* recipients */
$to  = "youremail@example.com"; // who is the email going to

/* subject */
$subject = "Example HTML E-Mail";

/* message */
$message = '
<html>
<head>
 <title>Example HTML E-mail</title>
</head>
<body>
<table>
 <tr>
  <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
 </tr>
 <tr>
  <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
 </tr>
 <tr>
  <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
 </tr>
</table>
</body>
</html>
';

/* To send HTML mail, you can set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>
All you need to do is change the variables to suit the form that you are passing to this page such as the $subject or $message.
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Duff Rocks

Post by mccommunity »

Thanks a lot duff I really appreciate it.


Mark
niceguyeddie
Forum Newbie
Posts: 13
Joined: Fri Nov 28, 2003 7:12 am
Location: Westminster, MD

Post by niceguyeddie »

Even easier is using the PHPMailer class.

http://phpmailer.sourceforge.net/
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

niceguyeddie wrote:Even easier is using the PHPMailer class.

http://phpmailer.sourceforge.net/
I'm new to classes in PHP. Must I do any thing special to use the class, or just keep the file in the same folder as my php scripts?
niceguyeddie
Forum Newbie
Posts: 13
Joined: Fri Nov 28, 2003 7:12 am
Location: Westminster, MD

Post by niceguyeddie »

microthick wrote:I'm new to classes in PHP. Must I do any thing special to use the class, or just keep the file in the same folder as my php scripts?
This is straight from the readme:

Code: Select all

Copy class.phpmailer.php into your php.ini include_path. If you are
using the SMTP mailer then place class.smtp.php in your path as well.

Example

<?php
require("class.phpmailer.php");

$mail = new phpmailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "smtp1.site.com;smtp2.site.com";        // specify main and backup server
$mail->SMTPAuth = true     // turn on SMTP authentication
$mail->Username = "jswan"  // SMTP username
$mail->Password = "secret" // SMTP password

$mail->From = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com", "Josh Adams");
$mail->AddAddress("ellen@site.com");                  // name is optional
$mail->AddReplyTo("info@site.com", "Information");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
&#123;
   echo "Message could not be sent. <br/><br/>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
&#125;

echo "Message has been sent";
?>
If you don't have access to the ini, you can do a couple of things, but the easiest would be to do a straight include into your script:

Code: Select all

include 'path/to/class.phpmailer.php';
I just find it easier to use the class than to mess around with the php mail functions to do what I want to do. Cuts down the coding time;)
Post Reply