Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

Sending mail using SMTP with PHPMailer

May 3rd, 2007 by Jon Moffet

PHPMailer is a simple PHP class for sending emails from your web application.

Unlike the default mail() function, PHPMailer supports sending email from your own authenticated SMTP server instead of relying on shared hosting server for sending mails.

PHPMailer is also less vulnerable to Email Injection Attack which is notorious with the default PHP mail() function

Other PHPMailer features includes :

  • Easy facility for sending attachment
  • Easy to use Object Oriented PHP class
  • Support Sending HTML Email from scripts
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Portable - it works on Linux/BSD/Windows platform without modification

Here's how to use PHPMailer in your scripts (snippets)

PHP:
  1. require("phpmailer/class.phpmailer.php");
  2.  
  3. //****
  4. //PLEASE CHANGE THE SETTINGS HERE !!!!
  5. //change settings here
  6. $your_email = "info@example.com";
  7. $your_smtp = "mail.example.com";
  8. $your_smtp_user = "info@example.com";
  9. $your_smtp_pass = "example_password";
  10. $your_website = "http://example.com";
  11. //****
  12.  
  13.  
  14. //get contact form details
  15. $name = $_POST['name'];
  16. $email = $_POST['email'];
  17. $url = $_POST['url'];
  18. $comments = $_POST['comments'];
  19.  
  20. $response="Name: $name\nContents:\n$comments\n";
  21.  
  22.  
  23. $mail = new PHPmailer();
  24.  
  25. $mail = $mail->SetLanguage("en", "phpmailer/language");
  26.  
  27. $mail->From = $your_email;
  28. $mail->FromName = $your_website;
  29. $mail->Host = $your_smtp;
  30. $mail->Mailer   = "smtp";
  31. $mail->Password = $your_smtp_pass;
  32. $mail->Username = $your_smtp_user;
  33. $mail->Subject = "$your_website feedback";
  34. $mail->SMTPAuth  =  "true";
  35.  
  36. $mail->Body = $response;
  37. $mail->AddAddress($your_email,"$your_website admin");
  38. $mail->AddReplyTo($email,$name);
  39.  
  40. echo "<p>Thanks for your feedback, <em>$name</em>! We will contact you soon!</p>";
  41. if (!$mail->Send()) {
  42. echo "<p>There was an error in sending mail, please try again at a later time</p>";
  43. }
  44.  
  45. $mail->ClearAddresses();
  46. $mail->ClearAttachments();

Note : Please change the SMTP server setting to your own server.

Although PHPMailer code seems a bit longer - it is much more readable and cleaner to write than the default mail() code.

Example code
Download example Contact Form code that uses PHPMailer : plaincf.zip (83KB)

Tags: , , , , , , , ,

Bookmark Post:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • NewsVine
  • Reddit
  • Netvouz
  • Spurl
  • Furl
  • digg
  • YahooMyWeb
  • del.icio.us

Posted in Uncategorized |

Related Posts

3 Responses

  1. Spam Protected Contact Form example (using Akismet) » Quick PHP Code Tips and Examples Says:

    [...] This is an example of an enhanced contact form previously previously featured here and here [...]

  2. Carlos F. Arce Says:

    THANK YOU VERY MUCH !!!!!! IT WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Like how we say in my country:
    THANK YOU TEACHER!!!!!!!!!!!!

  3. MaTaN Says:

    Thankyou this WORKS well just what I am looking for. It gets past that anoying mail error in the windows servers…Warning: mail() [function.mail]: Failed to connect to mailserver at “localhost” ERROR NO MORE :)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.