【问题标题】:PHP : send mail in localhostPHP:在本地主机中发送邮件
【发布时间】:2013-03-07 09:27:32
【问题描述】:

我想通过本地托管的 php 代码发送电子邮件。

<?php 
$email  = "myemail@local.com"; 
$titre   = "My subject"; 
$message = "Text message !"; 
mail($email, $titre, $message); 
?>

当我运行此代码时,我收到以下错误:

Warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\...

我进入了php.ini 文件,它似乎已经配置好了。

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

我该如何解决这个问题?

谢谢

【问题讨论】:

  • 你不能从本地主机发送邮件,在本地主机eg : google,yahoo...配置一些其他的smtp
  • "failed to connect to mailserver"...你已经配置PHP连接到本地主机上的邮件服务器,端口25。你安装了这样的邮件服务器吗?
  • @YogeshSuthar — 不正确!任何连接到网络的计算机都可以发送电子邮件。
  • @YogeshSuthar — 如果您安装 SMTP 服务器并将其配置为侦听该端口 - 是的。
  • @YogeshSuthar — 那里有很多免费的 SMTP 服务器。我通常使用 Postfix。不过,使用您的服务提供商的服务器可能是一个更好的主意。维护 SMTP 服务器可能很棘手(考虑到垃圾邮件发送者会破坏生态系统)。

标签: php email


【解决方案1】:

配置为使用localhost:25作为邮件服务器。

错误消息说它无法连接到localhost:25

因此你有两个选择:

  1. 在 localhost 端口 25 上安装/正确配置 SMTP 服务器
  2. 更改配置以指向您可以连接到的其他 SMTP 服务器

【讨论】:

  • 不是一个很有帮助的答案。你基本上说如果它不工作,那么让它工作或让另一个服务器工作。
  • @mgrenier — 与其他答案相反?产品推荐将是题外话。
  • 与@mgrenier 的评论相反,我发现这可能是最基本但内容丰富的在线回复,哈哈。 “它坏了”......“修复它”对我来说很简单。
  • 您在 OP 下的 cmets 也非常有帮助,可能值得您回答。干杯!
【解决方案2】:

我在这上面花了几个小时。我以前没有收到错误,但从未发送过邮件。终于找到了解决办法,分享给大家。

<?php
include 'nav.php';
/*
    Download PhpMailer from the following link:
    https://github.com/Synchro/PHPMailer (CLick on Download zip on the right side)
    Extract the PHPMailer-master folder into your xampp->htdocs folder
    Make changes in the following code and its done :-)

    You will receive the mail with the name Root User.
    To change the name, go to class.phpmailer.php file in your PHPMailer-master folder,
    And change the name here: 
    public $FromName = 'Root User';
*/
require("PHPMailer-master/PHPMailerAutoload.php"); //or select the proper destination for this file if your page is in some   //other folder
ini_set("SMTP","ssl://smtp.gmail.com"); 
ini_set("smtp_port","465"); //No further need to edit your configuration files.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPSecure = "ssl";
$mail->Username = "trials.php@gmail.com"; //account with which you want to send mail. Or use this account. i dont care :-P
$mail->Password = "trials.php.php"; //this account's password.
$mail->Port = "465";
$mail->isSMTP();  // telling the class to use SMTP
$rec1="trials.php@gmail.com"; //receiver. email addresses to which u want to send the mail.
$mail->AddAddress($rec1);
$mail->Subject  = "Eventbook";
$mail->Body     = "Hello hi, testing";
$mail->WordWrap = 200;
if(!$mail->Send()) {
echo 'Message was not sent!.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo  //Fill in the document.location thing
'<script type="text/javascript">
                        if(confirm("Your mail has been sent"))
                        document.location = "/";
        </script>';
}
?>

【讨论】:

  • 找不到 PHPMailerAutoload.php
  • 这是一个很好的答案,但如果我需要用非英文字母书写主题怎么办?因为我试图这样做,我收到了一封电子邮件,里面有מייל 之类的字母。
【解决方案3】:

您需要安装本地邮件服务器才能执行此操作。 如果您想将它发送到外部电子邮件地址,它可能会以不需要的电子邮件结束,或者根本无法到达。

我使用的一个好的邮件服务器(我在 Linux 上使用它,但它也可用于 Windows)是 Axigen: http://www.axigen.com/mail-server/download/

您可能需要一些邮件服务器方面的经验才能安装它,但是一旦它工作,您就可以用它做任何您想做的事情。

【讨论】:

    【解决方案4】:

    试试这个

    ini_set("SMTP","aspmx.l.google.com");
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "From: test@gmail.com" . "\r\n";
    mail("email@domain.com","test subject","test body",$headers);
    

    【讨论】:

      【解决方案5】:

      可以在不使用任何繁重的库的情况下发送电子邮件,我在这里包含了我的示例。

      用于 PHP 的轻量级 SMTP 电子邮件发件人

      https://github.com/Nerdtrix/EZMAIL

      在生产和开发环境中都经过测试。

      最重要的是,除非您的 IP 被服务器列入黑名单,否则电子邮件不会成为垃圾邮件。

      干杯。

      【讨论】:

        猜你喜欢
        • 2013-08-19
        • 2016-01-31
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        • 2013-10-01
        • 2010-12-19
        相关资源
        最近更新 更多