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: , , , , , , ,

Posted in Uncategorized | 4 Comments »

Spam Protected Contact Form example (using Akismet)

May 9th, 2007 by Jon Moffet

This is an example of an enhanced contact form previously featured here and here . I've improved the contact form example to include Akismet spam protection in order to make it more resistant to spam attack.

Demonstration

plain_akismet.jpg

The example includes :

  • PHPMailer class for sending mail through your own SMTP server
  • Akismet Anti-spam class
  • Table-less contact form

Feel free to download the example to be used in your own application : plain_akismet.zip

Tags: , , , , , , ,

Posted in Uncategorized | 2 Comments »

How to switch to PHP5 in Cpanel based web hosting

May 9th, 2007 by Jon Moffet

Some web hosting provider such as e-ruang offers choices of using PHP4 or or PHP5 in their hosting. To avoid conflict with existing scripts, they will usually offer PHP 4 by default.

But for obvious reasons, you may want to use PHP5 as it supports more functionalities and more robust than PHP4. To enable PHP5 for your account, you need to create a .htaccess file containing :

CODE:
  1. AddHandler application/x-httpd-php5 .php .php4 .php3 .phtml

Save the .htaccess file, and upload it to the root directory of your server.

Then upload a php file containing phpinfo() function to test whether PHP5 has been enabled or not. If the modification has not been successful then you should contact your web hosting provider and ask about the appropriate way to activate PHP5 functionality.

Tags: , , ,

Posted in Uncategorized | No Comments »

Simple Contact form example PHP

May 3rd, 2007 by Jon Moffet

I've manage to create a simple Contact Form demonstration code for you to use as an example. The code is very minimal and consist of 3 main files :

  • contactform.html - for the contact form interface
  • contact.css - for styling the contact form
  • process.php - form processing code and logic

The example uses PHPMailer to send emails instead of mail() function as it is safer to use than the usual mail() function

Note : the contact form is very simple that it does not include input validation to counter spammers and malicious user, use it for your own reference only.

Download link : plaincf.zip
Demo : Plain Contact Form

Simple Contact Form Example Source code

Tags: , , , , , ,

Posted in Uncategorized | 4 Comments »

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: , , , , , , , ,

Posted in Uncategorized | 8 Comments »

« Previous Entries