在本地主机中使用php发送电子邮件

时间:2012-02-27 15:10:06

标签: php email xampp

我想使用xampp发送带有php的电子邮件。

<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,我不明白。

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

PHP需要SMTP服务器才能发送电子邮件。

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

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

答案 2 :(得分:0)

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

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

  

SMTP = smtp.yourprovider.com

     

SMTP_PORT = 25

从PHP邮件联机帮助页

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

答案 3 :(得分:0)

答案 4 :(得分:0)

 <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";

  }
      ?>

答案 5 :(得分:0)

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

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