test_db_smtp_basic.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <html>
  2. <head>
  3. <title>PHPMailer - MySQL Database - SMTP basic test with authentication</title>
  4. </head>
  5. <body>
  6. <?php
  7. //error_reporting(E_ALL);
  8. error_reporting(E_STRICT);
  9. date_default_timezone_set('America/Toronto');
  10. require_once('../class.phpmailer.php');
  11. //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
  12. $mail = new PHPMailer();
  13. $body = file_get_contents('contents.html');
  14. $body = eregi_replace("[\]",'',$body);
  15. $mail->IsSMTP(); // telling the class to use SMTP
  16. $mail->Host = "smtp1.site.com;smtp2.site.com";
  17. $mail->SMTPAuth = true; // enable SMTP authentication
  18. $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
  19. $mail->Host = "mail.yourdomain.com"; // sets the SMTP server
  20. $mail->Port = 26; // set the SMTP port for the GMAIL server
  21. $mail->Username = "yourname@yourdomain"; // SMTP account username
  22. $mail->Password = "yourpassword"; // SMTP account password
  23. $mail->SetFrom('list@mydomain.com', 'List manager');
  24. $mail->AddReplyTo('list@mydomain.com', 'List manager');
  25. $mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
  26. @MYSQL_CONNECT("localhost","root","password");
  27. @mysql_select_db("my_company");
  28. $query = "SELECT full_name, email, photo FROM employee WHERE id=$id";
  29. $result = @MYSQL_QUERY($query);
  30. while ($row = mysql_fetch_array ($result)) {
  31. $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  32. $mail->MsgHTML($body);
  33. $mail->AddAddress($row["email"], $row["full_name"]);
  34. $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");
  35. if(!$mail->Send()) {
  36. echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
  37. } else {
  38. echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;", $row["email"]) . ')<br />';
  39. }
  40. // Clear all addresses and attachments for next loop
  41. $mail->ClearAddresses();
  42. $mail->ClearAttachments();
  43. }
  44. ?>
  45. </body>
  46. </html>