【问题标题】:send email with php in a local host在本地主机中使用 php 发送电子邮件
【发布时间】:2012-03-17 01:27:13
【问题描述】:

我想通过 php 使用 xampp 发送电子邮件。

<html>
   <head>

   </head>
   <body>

   <?php
    error_reporting(E_ALL ^ E_NOTICE);
    $to = $_POST['email_add'];
    //define the subject of the email
    $subject = $_POST['sbjct']; 
    //define the message to be sent. Each line should be separated with \n
    $message = $_POST['msg']; 
    //define the headers we want passed. Note that they are separated with \r\n
    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";


    $mail_sent = @mail( $to, $subject, $message, $headers );
    echo $mail_sent ? "Mail sent" : "Mail failed";
    ?>
    <form action="send_email.php" method="post">
    Email Add<input type="text" name="email_add" />
    Subject<input type="text" name="sbjct" /> <br>
     <textarea name="msg" rows="9"  cols="20"></textarea><br>

    <input type="submit" value="Send"/>
    </form>

    </body>

    </html>

它不工作。我的代码有问题吗?

在云上找了一些教程,但是里面提到了SMTP,我没看懂。

【问题讨论】:

  • 将error_reporting(E_ALL ^ E_NOTICE); 更改为error_reporting(E_ALL); 并从@mail() 中删除@。您可能需要设置经过身份验证的 SMTP。
  • 你可以在xammp中使用mercury邮件服务器。

标签: php email xampp


【解决方案1】:

这是我在很多项目中重复使用的脚本,非常简单,只需复制并更改明显的值即可。

$email_to = "myemail@gmail.com,anotheremail@gmail.com";
        $email_subject = "This is the subject line";

        $break = 'echo "/n"';

        $form_email = "no-reply@myemail.com";

        //Function to convert /n to line breaks in HTML
        function nl2br2($email_message) {
           $string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
           return $email_message;
        }

        //Unformatted email body
        $email_message = "This is the main blody of the email";

        //Format string against function
        nl2br2($email_message);

        // create email headers
        $headers = 'From: '.$form_email."\r\n".
            'Reply-To: '.$form_email."\r\n" .
            'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);

【讨论】:

    【解决方案2】:
     <html>
      <head>
    
       </head>
      <body>
    
    
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    Email Add<input type="text" name="email_add" />
    Subject<input type="text" name="sbjct" /> <br>
     <textarea name="msg" rows="9"  cols="20"></textarea><br>
    
      <input type="submit" name="submit" value="Send"/>
    </form>
    
    </body>
    
    </html>
    
     <?php
        If(isset('submit'))
     {
       error_reporting(E_ALL);
       $to = $_POST['email_add'];
       //define the subject of the email
       $subject = $_POST['sbjct']; 
       //define the message to be sent. Each line should be separated with \n
        $message = $_POST['msg']; 
        //define the headers we want passed. Note that they are separated with \r\n
        $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
    
    
    $mail_sent = mail( $to, $subject, $message, $headers );
    echo $mail_sent ? "Mail sent" : "Mail failed";
    
      }
          ?>
    

    【讨论】:

      【解决方案3】:

      PHP 需要一个 SMTP 服务器来发送电子邮件。

      您可以在您的 php 配置文件中指定它(例如,将其设置为您的 ISP 之一),或使用“测试邮件服务器工具”(http://www.toolheap.com/test-mail-server-tool/)捕获 smtp 本地调用并将它们保存在您的硬盘,以便您可以调试所有内容。

      显然,此解决方案仅在您需要调试时才有效。 如果您需要发送邮件,请参考 php 文档并使用您的 ISP SMTP 服务器。

      【讨论】:

      • +1。我在 Win/XP 上使用 XAMPP 时从来没有遇到过这个问题,但是当我切换到 Windows 7 时,它缺少本地 smtp 服务器。安装了一个测试 smtp 服务器,一切都很好。
      【解决方案4】:

      【讨论】:

        【解决方案5】:

        如果您在本地开发环境中使用 Windows 上的 mail() 函数,您可能需要指定一个可以为您中继邮件的 SMTP 服务器。 Linux 具有此功能,Windows 可能没有。您的 ISP 可能会为您提供此服务,或者网络邮件提供商可能允许您通过他们转发邮件。

        查看您的 php.ini 文件并找到 [mail] 部分并填写:

        SMTP = smtp.yourprovider.com

        SMTP_PORT = 25

        来自 PHP 邮件手册页

        “mail() 的 Windows 实现在许多方面与 Unix 实现不同。首先,它不使用本地二进制文件来编写消息,而仅在直接套接字上运行,这意味着需要 MTA 侦听网络套接字(可以在本地主机或远程机器上)。”

        【讨论】:

          【解决方案6】:

          PHP 实际上并不发送电子邮件...在 Linux 中它会将其转发到 sendmail 或类似的守护进程,在 Windows 中它会将其转发到 SMTP 服务器时间>。根据您的操作系统,您必须设置这些其他进程并向 PHP 解释如何/在何处联系它们(在 [mail function] 部分的 php.ini 中)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-10-01
            • 2010-12-19
            • 2017-05-09
            • 2016-07-22
            • 2016-11-02
            相关资源
            最近更新 更多