1 <?php
2$pp = popen("/usr/sbin/sendmail -t", "w") or die("Cannot fork sendmail");
3fputs($pp, "To: sterling@designmultimedia.com\r\n");
4fputs($pp, "Reply-to: $senders_email\r\n");
5fputs($pp, "From: $senders_email\r\n");
6fputs($pp, "Subject The Results of your form\r\n\r\n");
7fputs($pp, "$senders_email sent the fllowing comments:\r\n");
8fputs($pp, $comments);
9pclose($pp) or die("Cannot close pipe to sendmail");
10 ?>
Examples using phpmailer 1. Advanced ExampleThis demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.
1require("class.phpmailer.php");
2 3$mail = new phpmailer();
4 5$mail->From = "list@example.com";
6$mail->FromName = "List manager";
7$mail->Host = "smtp1.example.com;smtp2.example.com";
8$mail->Mailer = "smtp";
910 @MYSQL_CONNECT("localhost","root","password");
11 @mysql_select_db("my_company");
12$query?=?SELECT full_name, email,?hoto?ROM employee?HERE?d=$id";
13$result??MYSQL_QUERY($query);
1415 while ($row = mysql_fetch_array ($result))
16{
17 // HTML body
18$body = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
19$body .= "<i>Your</i> personal photograph to this message.<p>";
20$body .= "Sincerely, <br>";
21$body .= "phpmailer List manager";
2223 // Plain text body (for mail clients that cannot read HTML)
24$text_body = "Hello " . $row["full_name"] . ", \n\n";
25$text_body .= "Your personal photograph to this message.\n\n";
26$text_body .= "Sincerely, \n";
27$text_body .= "phpmailer List manager";
2829$mail->Body = $body;
30$mail->AltBody = $text_body;
31$mail->AddAddress($row["email"], $row["full_name"]);
32$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");
3334 if(!$mail->Send())
35 echo "There has been a mail error sending to " . $row["email"] . "<br>";
3637 // Clear all addresses and attachments for next loop
38$mail->ClearAddresses();
39$mail->ClearAttachments();
40 }
2. Extending phpmailerExtending classes with inheritance is one of the most powerful features of object-oriented programming. It allows you to make changes to the original class for your own personal use without hacking the original classes. Plus, it is very easy to do. I’ve provided an example:?
Here’s a class that extends the phpmailer class and sets the defaults for the particular site:? PHP include file: mail.inc.php?
require("class.phpmailer.php");?
1class my_phpmailer extends phpmailer {
2// Set default variables for all new objects 3var$From = "from@example.com";
4var$FromName = "Mailer";
5var$Host = "smtp1.example.com;smtp2.example.com";
6var$Mailer = "smtp"; // Alternative to IsSMTP() 7var$WordWrap = 75;
8 9// Replace the default error_handler 10function error_handler($msg) {
11print("My Site Error");
12print("Description:");
13printf("%s", $msg);
14exit;
15 }
1617// Create an additional function 18function do_something($something) {
19// Place your new code here 20 }
21 }
Now here’s a normal PHP page in the site, which will have all the defaults set above:? Normal PHP file: mail_test.php?
1require("mail.inc.php");
2 3// Instantiate your new class 4$mail = new my_phpmailer;
5 6// Now you only need to add the necessary stuff 7$mail->AddAddress("josh@example.com", "Josh Adams");
8$mail->Subject = "Here is the subject";
9$mail->Body = "This is the message body";
10$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip"); // optional name 1112if(!$mail->Send())
13{
14echo "There was an error sending the message";
15exit;
16}
1718echo "Message was sent successfully";