Download the PHPMailer script (updated for simplicity)
First, download PHPMailer using the direct link below:
PHPMailer_Parallax.zipAfter you have downloaded the file, unzip and extract it to your public_html. After unzipping the file we have public_html/PHPMailer_Parallax.zip. Next you will need to edit your web pages to use the PHPMailer code.
In PHPMailer_Parallax.zip file, it has a file "
test_phpmailer_call.php" and a folder "
phpmailer".
You need to add folder "
phpmailer" and call a custom function
smtpmailer() to send mail.
Add the HTML form to your pageGenerally, your setup would include a form being sent to a PHP script for processing. In this case, we have added a file "
test_phpmailer_call.php" within
PHPMailer_Parallax.zip to call a custom function to send an email.
test_phpmailer_call.php code:Because we are using the PHPMailer instead of the generic php mail function, we'll begin to update our "
test_phpmailer_call.php" file.
The sample PHP code should look like below:
<?php
require_once('phpmailer/phpmailer.function.php');
if (smtpmailer($to, $from, $name, $subj, $msg, false)) {
// do something
}
if (!empty($error)) echo $error;
?>
The great thing about this example is that it includes comments, which explain what most of the code does.
Final Configuration of the PHPMailer codeNow we have to setup email id, email password & smtp hostname within folder "
phpmail/phpmailer.function.php". We updated the code to include additional comments, and the final code we used is below.
<?php
require_once('class.phpmailer.php');
define('MAIL_USER', '[email protected]'); // email id
define('MAIL_PASSWORD', 'password'); // email password
define('SMTP_SERVER', 'mail.example.com'); // sec. smtp server
function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail=false) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
if ($is_gmail) {
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = MAIL_USER;
$mail->Password = MAIL_PASSWORD;
} else {
$mail->Host = SMTP_SERVER;
$mail->Username = MAIL_USER;
$mail->Password = MAIL_PASSWORD;
$mail->Port = 25;
}
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
?>
So in the test the user first visits
test_phpmailer_call.php.
Note that PHPMailer is very flexible and has more features than described here. For a listing of the optional variables that you can add, you should read the documentation:
http://PHPMailer.sourceforge.net/docs/