简体   繁体   English

尝试在php中发送邮件时出现错误

[英]Getting error while trying to send mail in php

I am getting the error message as Failed to connect to mailserver at &quot,localhost&quot, port 25,... 我收到错误消息,因为无法连接到“ localhost”端口25的邮件服务器...

Contents of my php.ini file are 我的php.ini文件的内容是

SMTP = localhost
smtp_port = 25

I used the following code 我用下面的代码

mail("xyz@gmail.com","test","msg","from abc@gmail.com");

You can't send outbound mails from localhost. 您不能从本地主机发送出站邮件。 To test the mail function, install mercury mail. 要测试邮件功能,请安装水银邮件。 It should come with xampp. 它应该随xampp一起提供。 Create emails for the localhost domains like steward@localhost. 为steward @ localhost之类的localhost域创建电子邮件。 YOu could use aliases. 您可以使用别名。 Do your testing with that sending mails from one inbox to the other. 通过将邮件从一个收件箱发送到另一个收件箱来进行测试。 You'd need a licensed version of mercury mail to send outbound messages. 您需要汞邮件的许可版本才能发送出站邮件。

Another option is to run your test on a remote server. 另一个选择是在远程服务器上运行测试。 Make sure the senders email is recognised by the sending server. 确保发件人的电子邮件被发送服务器识别。 Like you cannot be sending a gmail message using those settings you are displaying. 就像您无法使用显示的设置发送gmail消息一样。

If sending from gmail is your objective, stackoverflow is full of answers already for how to send mails with gmail, even with codeigniter. 如果从gmail发送是您的目标,那么stackoverflow已经充满了有关如何使用gmail发送邮件的答案,即使使用codeigniter也是如此。 Need some? 需要一些?

Here is what you ask for: Sending mail using gmail: 这是您的要求:使用gmail发送邮件:

   require_once "Mail.php";

    $from = "<from.gmail.com>";
    $to = "<to.yahoo.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "myaccount@gmail.com";  //<> give errors
    $password = "password";

    $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>");
     }

?>  <!-- end of php tag-->

Get info here: https://stackoverflow.com/a/2748837/827224 在这里获取信息: https : //stackoverflow.com/a/2748837/827224

You could even do more if you are using codeigniter or any PHP email class like PHPMailer search email classes in phpclasses.org. 如果您正在使用codeigniter或phpclasses.org中的任何PHP电子邮件类(如PHPMailer搜索电子邮件类),甚至可以做更多的事情。 An example is: http://www.phpclasses.org/blog/package/9/post/1-Sending-email-using-SMTP-servers-of-Gmail-Hotmail-or-Yahoo-with-PHP.html 一个示例是: http : //www.phpclasses.org/blog/package/9/post/1-Sending-email-using-SMTP-servers-of-Gmail-Hotmail-or-Yahoo-with-PHP.html

Finally, here is a class you can use: 最后,这是您可以使用的类:

http://www.phpclasses.org/package/7834-PHP-Send-e-mail-messages-Gmail-users-via-SMTP.html http://www.phpclasses.org/package/7834-PHP-Send-e-mail-messages-Gmail-users-via-SMTP.html

Its clearly evident that there is no mail server running on localhost on port 25 (or a firewall is blocking it). 很明显,在本地主机的端口25上没有运行邮件服务器(或者防火墙阻止了它)。 Get and install a mailserver (there are a number of free ones for windows/linux/mac - just make sure your ISP allows it) and your script will run just fine. 获取并安装一个邮件服务器(对于Windows / linux / mac,有很多免费的-只需确保您的ISP允许它即可),您的脚本就可以正常运行。

I use this for testing which is quite nice: http://smtp4dev.codeplex.com/ Its a fake email server, that intercepts mail and dumps them for you to inspect and test on localhost. 我将其用于测试,这非常不错: http : //smtp4dev.codeplex.com/它是一个伪造的电子邮件服务器,它拦截邮件并将其转储以供您在localhost上进行检查和测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM