Configuring MAIL with PEAR and PHPMAILER

ABSTRACT

I am going to describe hereunto the different steps you would need to take in order to allow the mail system that we have, and your scripts to work together. The current system uses SMTP authentication, more secure and aimed at preventing the effects and consequences from spammers. This is aimed at Windows users mainly.

MAIN

There are 2 popular ways of doing it. One is using the PEAR library and the other is using another application called PHP MAILER.

The PEAR library

is already present in the server, and it is installed “server wide”, this meaning that you don’t have to worry about writing a path to the mail.php script. The following GUI is only visible in the Linux server and you don’t need to use SMTP authentication in this case. This said, all you need to do is select the files you need and press a button.

Here is a screenshot picture of all the things you will need to do click on:

Once you have that installed, there are as a minimum, (there could be more if you want to add real time validation scripts, usually written in JS), two files that you will need to work with: The HTML form for the user interface, and the processing script in PHP.

Here there can be several different cases, if you are using any CMS such as Joomla, you would need to go to the System configuration of the CMS and select the correct options. These correct options are the ones which we are going to see now, for the case of manual configuration, that is, a PHP script that you have written yourself or found elsewhere. So, once this is done, it will solve all scenarios.

I am copying and pasting the PHP file that I have. You would just need to put your info instead of mine.

<?php
require_once “Mail.php”;  AS I MENTIONED, NO NEED TO WRITE PATHS

$from = “Sandra Sender <sender@example.com>”;THIS IS WHAT YOUR VISITOR WILL FILL IN THE FIELD
$to = “NAME <putyourname@putyourdomain.com>”; THIS IS TO WHERE YOU WANT THE MAIL TO BE SENT TO
$subject = “Hi!”; THIS SHOULD RECEIVE THE CONTENT FROM THE SUBJECT FIELD
$body = “Hi,\n\nHow are you?”; RECEIVING CONTENT FROM THE BODY, THE MESSAGE ACTUALLY

$host = “mail.PUTYOURDOMAIN.com”;
$port = 26; NOTICE THIS PORT HAS TO BE 26, NOT 25
$username = “PUTYOURNAME+PUTYOURDOMAIN.com”;YES, THAT IS  A “PLUS” SIGN
$password = “YOUR PWD”;

$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘port’ =>$port,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo(“<p>” . $mail->getMessage() . “</p>”);
} else {
echo(“<p>Message successfully sent!</p>”);
}
?>

/*************************************PHP MAILER ************************************************/

This pack needs to be fetched from third parties. Particularly I found it at Softpedia. The author of the script in his website redirects you to SourceForge. SourceForge presented me with other advertised programs which I did not want nor did I need, but did not present me the one I wanted, PHPMAILER, therefore I found it at Softpedia, as I said.

You will obtain said file in a zipped form. You can upload it to public_html, like that and unzip it remotely, the system does it for you and this is the recommended way to do it.

Once you have unzipped it, you will see several folders. You would want to go to Examples and select the pre-made script, called test_smtp_basic.php, which is ready to go. All you need to do is fill in the data that applies to your coordinates, domain, pwd etc. I copy and paste again what my file looks like.

<html>
<head>
<title>PHPMailer – SMTP basic test with authentication</title>
</head>
<body>

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set(‘America/Toronto’);

require_once(‘../class.phpmailer.php’);AS YOU SEE THERE IS A CALL TO ANOTHER FILE IN ANOTHER DIRECTORY
//include(“class.smtp.php”); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents(‘contents.html’);
$body             = eregi_replace(“[\]“,”,$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = “mail.YOURDOMAIN.COM “; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = “mail.YOURDOMAIN.COM”; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = “NAME+DOMAINl.net”; // SMTP account username
$mail->Password   = “PWD”;        // SMTP account password

$mail->SetFrom(‘NAME@DOMAIN.net’, ‘First Last’);

$mail->AddReplyTo(“NAME@DOMAIN.net”,”First Last”);

$mail->Subject    = “PHPMailer Test Subject via smtp, basic with authentication”;

$mail->AltBody    = “To view the message, please use an HTML compatible email viewer!”; // optional, comment out and test

$mail->MsgHTML($body);

$address = “NAME@DOMAIN.net”;
$mail->AddAddress($address, “John Doe”);

$mail->AddAttachment(“images/phpmailer.gif”);      // attachment
$mail->AddAttachment(“images/phpmailer_mini.gif”); // attachment

if(!$mail->Send()) {
echo “Mailer Error: ” . $mail->ErrorInfo;
} else {
echo “Message sent!”;
}

?>

</body>
</html>

ACKNOWLEDGEMENTS

The knowledge included here is due, to a generous extent, to the help of the support staff from ARVIXE, as I sent my queries at different steps of my own installation and running. So, big credit goes to them.

Tags: , , , , , , , | Posted under Mail, Webmail | RSS 2.0

4 Comments on Configuring MAIL with PEAR and PHPMAILER

  1. Billy says:

    This was very helpful, especially since this demonstrates how PHPMailer can easily work on Arvixe hosted sites. This got me working in minutes, thanks so much :)

  2. Tammy Siluis says:

    I would like to thnkx for the efforts you’ve put in writing this site. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has encouraged me to get my own website now. Actually the blogging is spreading its wings rapidly. Your write up is a great example of it.

  3. Recep Kurt says:

    Hello,
    First of all, i tried and it worked. Thank you for your great post.
    And,
    Can you explain how can i send an e-mail with HTML codes ?
    I want to send it using PEAR.

    Best Regards.

  4. ProfiAnts says:

    It is too complicated. I’m trying to understand what should I do to make the contact form work, but no success.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 3 = 7

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>