Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

How to send e-mail with Attachment with PHP Scripts

May 15th, 2007 by Jon Moffet

Here's how to send an email with attachment using PHP scripts (via PHPMailer class).

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

the key function of this script is $mail->AddAttachment("picture.png","picture.png");, where you can add any file as email attachments from your script.

Hopefully this example will help you to create a useful and flexible mail sending php scripts

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

4 Responses

  1. Bill Compton Says:

    Hi Jim. Photos i received. Thanks

  2. primo35711 Says:

    links from Technoratihttp://phpcode.mypapit.net/php-class/how-to-send-e-mail-with-attachment-with-php-scripts/43/

  3. levygapody Says:

    Hello! Good Site! Thanks you! qqkxlwipnlr

  4. lsjlkzomnh Says:

    I’ve never been all that big a fan of Hyundai and it could be because of the sour taste left from the Hyundai Pony so many years ago. Quality is definitely improving in South Korea and people are quite pleased with the 2007/2008 Tiburon. We’ve seen some heavily camouflaged pictures of its RWD replacement, but when there’s that much tarp, it’s hard to decide whether you like the new car or not. It’s time to strip away all that camouflage and see what the next generation Hyundai Tiburon replacement is all about. Here is the Hyundai Coupe in…http://super-blog-here.com/

Leave a Comment

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