0

我正在使用 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!
}

?>
4

3 回答 3

1

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

yum install postfix

并在此处查看文档:

http://www.postfix.org/docs.html
于 2009-11-14T23:32:25.733 回答
1

我强烈建议使用PHPMailer 之类的库来发送电子邮件。

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

为了更好的主意,请从他们的网站上查看此示例:

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!
}

编辑2:

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

代替

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

和:

$mail->Host = "smtp.gmail.com";
于 2009-11-15T00:01:02.757 回答
0

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

于 2009-11-14T23:30:49.347 回答