我的PHP邮件功能不起作用

时间:2016-09-04 18:41:41

标签: php html post get phpmailer

我正在尝试创建一个网站,允许用户通过 php" mail"通过电子邮件发送联系人列表。功能即可。 "表单操作" 似乎已激活但是当我检查我的邮件时它没有显示,我检查了垃圾邮件文件夹。

这是html代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>TTP Banking Portal</h2>
  <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
    <li><a data-toggle="tab" href="#withdraw">Withdraw</a></li>
    <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
    <li><a data-toggle="tab" href="#menu3">Menu 3</a></li>
    <li><a data-toggle="tab" href="#email">Email</a></li>
  </ul>

  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <h3>My Account</h3>
      <p>Welcome back, Tyler B. Grim</p>
      <h2>Account Details:</h2>
      <h3>
      <h2>Do you want to:</h2>
      <a data-toggle = "tab" href = "#withdraw">Withdraw</a>
    </div>
    <div id="withdraw" class="tab-pane fade">
      <h3>Menu 1</h3>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    <div id="menu2" class="tab-pane fade">
      <h3>Menu 2</h3>
      <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
    </div>
    <div id="menu3" class="tab-pane fade">
      <h3>Menu 3</h3>
      <p>Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
    </div>
    <div id="email" class="tab-pane fade">
      <form action = "../bin/portal.php?func_name=sendMail" method = "get">
        <p>Send Mail to:</p>
        <input type = "radio" name = "mailto" value = "tyler.psu.grim@gmail.com">tyler.psu.grim@gmail.com<br/>
        <input type = "radio" name = "mailto" value = "tyler.rko.grim@gmail.com">tyler.rko.grim@gmail.com<br/>
        <input type = "radio" name = "mailto" value = "tqg5198@psu.edu">tqg5198@psu.edu<br/>

        <p>Subject:</p>
        <input type = "text" name = "subject" placeholder = "Enter email subject:" />

        <p>Message Body</p>
        <input type = "text" name = "message" placeholder = "Enter email body here:" style="height:250px;width:1000px">

        <p>Signature</p>
        <input type = "text" name = "name" placeholder = "Enter name here"/>

        <br/>
        <br/>

        <input type = "submit">Send Mail</input>
  </div>
</div>

</body>
</html>

这是我的php功能代码:

<?php

function sendMail() {

    if (isset($_GET["mailto"])) {

        $to = $_GET["mailto"];

    }

    else {

        echo "<script type = 'text/javascript'>alert('No message recipient selected')</script>";

    }

    $message = $_GET["message"];
    $subject = $_GET["subject"];
    $name = $_GET["name"];

    if ($to) {

        $sendTo = $to;
        $sendSubject = $name . " " . $subject;
        $sendMessage = $message;
        if (mail($sendTo, $sendSubject, $sendMessage)) {

            echo "<script type = 'text/javascript'>alert('Message Sent')</script>";
        }
        else {

        echo "<script type = 'text/javascript'>alert('Your Computer Doesn't support PHP Mail Functionality.')</script>";
    }

    }

    else {

        echo "<script type = 'text/javascript'>alert('You need to enter a recipient before you can send an email')</script>";

    }
}

请注意,我已尝试使用&#34; POST&#34;而不是&#34; GET&#34;并没有解决问题。

更新我根据评论的建议对PHPMailer进行了更改:以下是修改后的代码,但我还没有收到电子邮件:

<?php

function sendMail() {

    date_default_timezone_set('Etc/UTC');
    require '..//bin//PHPMaster-mailer//PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "****************";
//Password to use for SMTP authentication
$mail->Password = "****************";
//Set who the message is to be sent from
$mail->setFrom($_GET["email"], $_GET["name"]);
//Set an alternative reply-to address
$mail->addReplyTo($_GET["email"], $_GET["name"]);
//Set who the message is to be sent to
$mail->addAddress($_GET["to"], $_GET["to"]);
//Set the subject line
$mail->Subject = $_GET["subject"];
$mail->AltBody = $_GET["message"];
//Attach an image file
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
}

我正在运行

Windows 7,XAMPP Server和网站正在localhost上运行。

我认为现在有可能localhost仍在查看旧的PHP代码。这是我提交表单时提交给我的浏览器(localhost)的链接。

http://localhost/Banking%20Portal/bin/portal.php?mailto=tyler.psu.grim%40gmail.com&subject=Test&message=test&name=Tyler

1 个答案:

答案 0 :(得分:-1)

如果您是从localhost运行它,它将无法正常工作。您无法从本地计算机向某人发送邮件。但您可以将其配置为从您的GMAIL /任何其他帐户向指定帐户发送邮件。

阅读this以获得进一步说明。