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

22 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 :)

  4. bayu Says:

    i have Message like this :

    Mailer Error: SMTP Error: Could not connect to SMTP host.

    i use host like this :

    $mail->Host = “smtp.telkom.net”;

    and

    $mail->Host = “mail.eforel.com”;

    still error if i remove
    $mail->Mailer = “smtp”;
    it’s work but my mail in the office not be show in bulk or spam

    please tell me what is’t error

  5. rely Says:

    Just want to thank you for sharing this. It was very very helpful.

  6. Php Mailer By Says:

    Kramer auto Pingback[...] • Sending mail using SMTP with PHPMailer » Quick PHP Code Tips and … [...]

  7. jasa seo Says:

    Many thanks! now i can test if there is error in my server email

  8. Black Says:

    Can I use this code that my website users can send me mail to my personal mail on Gmail ?

  9. Domain Broker - HTML - Site Templates - ThemeForest Says:

    Kramer auto Pingback[...] use PHPMailer. To use SMTP you’ll need to add some code. If you’re comfortable with PHP this short tutorial will get you [...]

  10. Email niet als account@server namens info@mijnwebsite.nl verzenden - SiteDeals Says:

    Kramer auto Pingback[...] een idee om hier http://phpcode.mypapit.net/sending-m…-phpmailer/26/ eens na te kijken. Phpmailer maakt het mogelijk zodat je als smtp gebruiker een email [...]

  11. [PHP] Ustawianie nadawcy wiadomoci email. Says:

    Kramer auto Pingback[...] Oglnie to ja bym poleca wysyanie maili przez smtp a nie funkcj mail. Polecam klas phpmailer: http://phpmailer.sourceforge.net/ Ma to takie zalety, e nie na wszystkich serwerach dziaa poprawnie funkcja mail, zawsze moe si okaza, e napiszesz skrypt a nagle admin pozmienia ustawienia i przestanie dziaa. Jak wysa maila przez smtp jest np tutaj: http://phpcode.mypapit.net/sending-mail-using-smtp-with-phpmailer/26/ [...]

  12. naren Says:

    sddddddddddddddddd

  13. Php Mailer Problem - PHP Says:

    Kramer auto Pingback[...] of info on it. To use smtp you will need an smtp server and Try this example and see if it helps: http://phpcode.mypapit.net/sending-m…-phpmailer/26/ R0bb0b Reputation Points: 359 Solved Threads: 85 Posting Shark Offline 947 [...]

  14. royaryAlkalia Says:

    Соберем для Вас по сети интернет базу данных
    потенциальных клиентов для Вашего Бизнеса
    (название, телефон, факс, email, сайт, имена и др информацию)
    Много! Быстро! Точно!
    Узнайте более подробную информацию по:
    Телефон +79133913837
    ICQ: 6288862
    Skype: prodawez3837
    Email: prodawez@mixmail.com

  15. You've been Stumbled! Says:

    Kramer auto Pingback[...] You’ve been Stumbled! [...]

  16. ravikumar Says:

    smtp mail working fine thanks :)

  17. how to solve Language string failed to load in phpmailer - CodeProject Says:

    Kramer auto Pingback[...] a look at the link mentioned below.   PHPMailer[^]   BR// Harsha   link   Posted 26 secs ago ZeeroC00l2.6K Add [...]

  18. butu bantuan mengenai website (html, css, javascript, php) ? ngumpul disini gan - Page 311 - Kaskus - The Largest Indonesian Community Says:

    Kramer auto Pingback[...] nah, abis ane googling, ane nemu solusi lain.. yaitu make class phpmailer().. ane nemunya di sini http://phpcode.my…iler/26/ terus yang mau ane nanyain, 1. kita tau dari mana host smtp-nya kita ?[di baris ke 7 gan...(link [...]

  19. Cheap Directory Submission Service Says:

    I want to know will it work for web directory ?

  20. phpmailer smtp - Virgilio Ricerca Web Says:

    Kramer auto Pingback[...] l'html …free-script.it/post/Script_php_I … -Altri risultati nel dominio -copia cacheSending mail using SMTP with PHPMailer Quick PHP Code Tips …Unlike the default mail() function, PHPMailer supports sending email from your own authenticated [...]

  21. [PhoenixPHP] Recuperacin de contrasea v1.2 Says:

    Kramer auto Pingback[...] #17 Hoy a las 10:01 am Normal (0) Para otros servers web te recomiendo usar: http://phpcode.mypapit.net/sending-mail-using-smtp-with-phpmailer/26/ ya que un servidor de smtp lo puedes consegir facilmente En [...]

  22. LKT - Acortar url facilmente Says:

    Kramer auto Pingback[...] http:// phpcode.mypapit.net/ sending- mail- using- smtp- with- phpmailer/ 26/ [...]

Leave a Comment

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