php
  • email
  • 2013-03-07 86 views 17 likes 
    17

    我想通过本地托管的PHP代码发送电子邮件。PHP:在本地主机发送邮件

    <?php 
    $email = "[email protected]"; 
    $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 
    

    我该如何解决这个问题?

    谢谢

    +0

    从本地主机本地主机'比如你不能发送邮件,配置一些其他SMTP:谷歌,雅虎......' – 2013-03-07 09:29:00

    +0

    “无法连接到邮件服务器” ......你已经配置PHP连接到本地主机上的邮件服务器,端口25.您是否安装了此类邮件服务器? – eis 2013-03-07 09:31:19

    +0

    你的运行系统是什么? – 2013-03-07 09:33:13

    回答

    12

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

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

    因此,你有两个选择:

    1. 安装/正确配置本地主机上的端口25
    2. 更改配置的SMTP服务器来指向一些其他的SMTP服务器,你可以连接到
    +13

    不是一个非常有用的答案。你基本上说如果它不工作,然后让它工作或使另一台服务器工作。 – mgrenier 2014-10-14 13:32:51

    +0

    @mgrenier - 与其他答案相反?产品推荐将成为主题。 – Quentin 2014-10-14 13:35:52

    +2

    与@mgrenier的评论相反,我发现这可能是最基本但信息量最大的在线响应。 “它的破产”......“修复它”对我来说足够简单。 – jjonesdesign 2015-06-17 03:07:03

    0

    试试这个

    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: [email protected]" . "\r\n"; 
    mail("[email protected]","test subject","test body",$headers); 
    
    +0

    这没有奏效。 – ravisoni 2013-08-13 06:04:25

    5

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

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

    您可能需要使用邮件服务器来安装它了一些经验,但一旦它的工作原理,你可以做任何你想要的东西。

    5

    我在这花了数小时。我曾经没有得到错误,但邮件从未被发送。最后,我找到了一个解决方案,我想分享它。

    <?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 = "[email protected]"; //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="[email protected]"; //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>'; 
    } 
    ?> 
    
    相关问题