
May 15th, 2007 by

Jon Moffet
Here's how to send an email with attachment using PHP scripts (via PHPMailer class).
CODE:
-
require("phpmailer/class.phpmailer.php");
-
//****
-
//PLEASE CHANGE THE SETTINGS HERE !!!!
-
//change settings here
-
$your_email = "info@example.com";
-
$your_smtp = "mail.example.com";
-
$your_smtp_user = "info@example.com";
-
$your_smtp_pass = "example_password";
-
$your_website = "http://example.com";
-
//****
-
//get contact form details
-
$name = $_POST['name'];
-
$email = $_POST['email'];
-
$url = $_POST['url'];
-
$comments = $_POST['comments'];
-
$response="Name: $name\nContents:\n$comments\n";
-
$mail = new PHPmailer();
-
$mail = $mail->SetLanguage("en", "phpmailer/language");
-
$mail->From = $your_email;
-
$mail->FromName = $your_website;
-
$mail->Host = $your_smtp;
-
$mail->Mailer = "smtp";
-
$mail->Password = $your_smtp_pass;
-
$mail->Username = $your_smtp_user;
-
$mail->Subject = "$your_website feedback";
-
$mail->SMTPAuth = "true";
-
$mail->Body = $response;
-
$mail->AddAddress($your_email,"$your_website admin");
-
$mail->AddReplyTo($email,$name);
-
-
$mail->AddAttachment("picture.png","picture");
-
-
echo "<p>Thanks for your feedback, <em>$name</em>! We will contact you soon!</p>";
-
if (!$mail->Send()) {
-
echo "<p>There was an error in sending mail, please try again at a later time</p>";
-
}
-
$mail->ClearAddresses();
-
$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: php, email, emails, mail, e-mails, programming, php scripts, scripts
Posted in Uncategorized |
4 Comments »

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

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: contact form, php, php4, akismet, antispam, anti-spam, example, coding
Posted in Uncategorized |
2 Comments »

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:
-
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: php5, php, web hosting, hosting
Posted in Uncategorized |
No Comments »

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

Tags: contact, contact form, example, examples, php, snippets, email
Posted in Uncategorized |
4 Comments »

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:
-
require("phpmailer/class.phpmailer.php");
-
-
//****
-
//PLEASE CHANGE THE SETTINGS HERE !!!!
-
//change settings here
-
$your_email = "info@example.com";
-
$your_smtp = "mail.example.com";
-
$your_smtp_user = "info@example.com";
-
$your_smtp_pass = "example_password";
-
$your_website = "http://example.com";
-
//****
-
-
-
//get contact form details
-
$name = $_POST['name'];
-
$email = $_POST['email'];
-
$url = $_POST['url'];
-
$comments = $_POST['comments'];
-
-
$response="Name: $name\nContents:\n$comments\n";
-
-
-
$mail = new PHPmailer();
-
-
$mail = $mail->SetLanguage("en", "phpmailer/language");
-
-
$mail->From = $your_email;
-
$mail->FromName = $your_website;
-
$mail->Host = $your_smtp;
-
$mail->Mailer = "smtp";
-
$mail->Password = $your_smtp_pass;
-
$mail->Username = $your_smtp_user;
-
$mail->Subject = "$your_website feedback";
-
$mail->SMTPAuth = "true";
-
-
$mail->Body = $response;
-
$mail->AddAddress($your_email,"$your_website admin");
-
$mail->AddReplyTo($email,$name);
-
-
echo "<p>Thanks for your feedback, <em>$name</em>! We will contact you soon!</p>";
-
if (!$mail->Send()) {
-
echo "<p>There was an error in sending mail, please try again at a later time</p>";
-
}
-
-
$mail->ClearAddresses();
-
$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: php, phpmailer, smtp, mail, contact form, spammers, injection, emails, email
Posted in Uncategorized |
8 Comments »