tags:

views:

566

answers:

3

I'm running fedora core 9 with php 5.2.6. I want to send a simple email from a form using the php mail command.

Do I need to configure my system to be an smtp server? If so do I have to load something like pear?

I've read that pear is installed by default. however when I go to /usr/lib/php/pear , the folder is empty.

When I go to install pear with yum install php-pear none of the mirrors are available.

Does anyone have any suggesions on what mail service to use? If it's installed by default and where I can figure out how to configure.

Thanks! Joe

This the code I'm using:

    <?php

require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);

$mail->IsSMTP();

try {
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
  $mail->Port       = "587";                 // set the SMTP port for the GMAIL server
  $mail->Username   = "[email protected]";  // GMAIL username
  $mail->Password   = "joeiscool";            // GMAIL password

  //This is the "Mail From:" field
  $mail->SetFrom('[email protected]', 'First Last');
  //This is the "Mail To:" field
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";

  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

?>
A: 

You can use the LAMPStack from BitNami. It comes with an easy installer that installs everything you need and you'll be running in minutes.

richard
A: 

The easiest way to get mail sending up and running is to install and configure postfix. You can use:

yum install postfix

And check out the documentation here:

http://www.postfix.org/docs.html
Travis
+1  A: 

I would strongly recommend using a library like PHPMailer to send emails.

You can use it with the server's own mail service or with any other server on the internet (your ISPs, Gmail, etc).

For a better idea, check this example from their website:

require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on     errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $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   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';     // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}


Edit:

Following up on your comment.. For a simple GMail example, try this:

require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);

$mail->IsSMTP();

try {
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
  $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
  $mail->Username   = "[email protected]";  // GMAIL username
  $mail->Password   = "yourpassword";            // GMAIL password

  //This is the "Mail From:" field
  $mail->SetFrom('[email protected]', 'First Last');
  //This is the "Mail To:" field
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";

  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

Edit2:

The reason why you're getting this error is because you have an extra ' on your string:

Replace

$mail->Host = "'smtp.gmail.com";

with:

$mail->Host = "smtp.gmail.com";
Carlos Lima
php mailer still requires sendmail setup correct? I get the error Could not execute: /usr/sbin/sendmail when testing.
Buffernet
If you don't have or don't want to install Sendmail, you can configure it to use any SMTP server on the internet. Just look at the example, you need to configure `$mail-IsSMTP();` and then `$mail->Host="smtp.server.com";` . You can even use GMail to send your emails if you want. Check this: http://phpmailer.worxware.com/index.php?pg=exampleagmail
Carlos Lima
BTW, I would recommend using your hosting/ISP/GMail mail servers instead of setting one up. That'd be one less thing for you to configure and **maintain**.
Carlos Lima
i set up to use gmail but i get the error: Could not execute: /usr/sbin/sendmail. check it at: http://buffernet.com/phpmailer2/test/testemail.php
Buffernet
Try this one.And make sure you remove the old `mail()` call you were using before from that page :)
Carlos Lima
i really appreciate the help: can you check http://buffernet.com/phpmailer2/test/testemail.php it says I can't connect to the gmail host.
Buffernet
Could you show us your code? It'd be hard to figure out what's wrong just by looking at the error message. Also, make sure you got all og the `$mail->SMTPAuth=true; $mail->SMTPSecure="tls"; $mail->Host="smtp.gmail.com"; $mail->Port=587;` settings correctly.
Carlos Lima
i posted my code in my original post.
Buffernet
I've updated my answer. Just remove the extra ' and you should be fine :)
Carlos Lima
i removed that from both post but apparently you saw it before I edited it. I removed it from the code and no change. Thanks for all of your help.
Buffernet
Any chance that your firewall is blocking the connections ?
Carlos Lima
yeah theres a chance what port would I unblock?
Buffernet
Outgoing: port 587
Carlos Lima