2016-09-04 49 views
1

我正在尝试创建一个网站,允许用户通过电子邮件发送邮件列表php“邮件”功能“表单动作”似乎激活,但是当我检查我的邮件它不显示,并且我检查垃圾邮件文件夹。我的PHP邮件功能不工作

下面是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 = "[email protected]">[email protected]<br/> 
     <input type = "radio" name = "mailto" value = "[email protected]">[email protected]<br/> 
     <input type = "radio" name = "mailto" value = "[email protected]">[email protected]<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>"; 

    } 
} 

请注意,我用 “POST”,而不是 “GET” 尝试和没有解决问题。

更新我所做的更改,根据的意见建议使用的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仍在查看旧的php代码。当我提交表单时,这里是提交给我的浏览器(localhost)的链接。

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

+0

你会得到什么警示? – RiggsFolly

+0

什么操作系统你运行这个,记住Windows不带有邮件服务器和PHP的'mail()'函数只传递邮件到邮件服务器它本身并没有发送邮件 – RiggsFolly

+0

我删除了phpMailer标签,因为你不是使用'phpMailer',但也许你应该是 – RiggsFolly

回答

-1

如果你从本地主机上运行此,它不会工作。您无法将邮件从本地计算机发送给某人。但你可以配置它从你的GMAIL /任何其他帐户发送邮件到指定的帐户。

阅读this作进一步澄清。

+0

是的,您只需要运行邮件服务器或使用SMTP客户端(例如PHPMailer)直接发送。 – Synchro

+0

你的SMTP服务器地址是什么? 127.0.0.1或计算机的临时IP地址?接收器应该如何回复? – Kandhan

+0

它将是您的计算机的外部地址之一。收件人的回复到达您域的MX记录时,与出站无关。 – Synchro