如何在fedora上使用php邮件

时间:2009-11-14 23:26:23

标签: php email fedora

我用php 5.2.6运行fedora core 9。我想使用php mail命令从表单发送一封简单的电子邮件。

我是否需要将我的系统配置为smtp服务器?如果是这样,我必须加载像梨这样的东西吗?

我已经读过默认安装的梨。但是当我去/ usr / lib / php / pear时,该文件夹是空的。

当我使用yum install php-pear安装pear时,没有镜像可供使用。

是否有人对使用哪种邮件服务有任何建议?如果它是默认安装的,我可以在哪里弄清楚如何配置。

谢谢! 乔

这是我正在使用的代码:

    <?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   = "joestestemail@gmail.com";  // GMAIL username
  $mail->Password   = "joeiscool";            // GMAIL password

  //This is the "Mail From:" field
  $mail->SetFrom('joestestemail@gmail.com', 'First Last');
  //This is the "Mail To:" field
  $mail->AddAddress('joe12345@mailcatch.com', '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!
}

?>

3 个答案:

答案 0 :(得分:1)

让邮件发送和运行的最简单方法是安装和配置postfix。您可以使用:

yum install postfix

请查看此处的文档:

http://www.postfix.org/docs.html

答案 1 :(得分:1)

我强烈建议您使用PHPMailer等库来发送电子邮件。

您可以将其与服务器自己的邮件服务或互联网上的任何其他服务器(您的ISP,Gmail等)一起使用。

为了更好的主意,请从他们的网站上查看this example

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('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', '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!
}

编辑:

跟进你的评论.. 有关简单的GMail示例,请尝试以下操作:

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   = "yourusername@gmail.com";  // GMAIL username
  $mail->Password   = "yourpassword";            // GMAIL password

  //This is the "Mail From:" field
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  //This is the "Mail To:" field
  $mail->AddAddress('whoto@otherdomain.com', '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:

您收到此错误的原因是因为您的字符串上有额外的'

替换

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

with:

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

答案 2 :(得分:0)

您可以使用LAMPStack from BitNami。它附带一个简单的安装程序,可以安装您需要的所有内容,并且您将在几分钟内运行。