2017-03-28 145 views
0

我一直在研究这个问题一段时间,而且对于PHP来说还是比较新的。我无法将其发送。这个代码的第二双眼睛将是最有帮助的:HTML/PHP从表单发送电子邮件

<?php 
    if(isset($_POST['submit'])){ 
     $to = "myEmail"; // this is your Email address 
     $from = $_POST['emailAddress']; // this is the sender's Email address 
     $fullName = $_POST['fullName']; 
     $subject = "Form submission"; 
     $message = $fullName . " wrote the following:" . "\n\n" . $_POST['comment']; 
     $message2 = "Here is a copy of your message " . $fullName . "\n\n" . $_POST['comment']; 

     $headers = "From:" . $from; 
     $headers2 = "From:" . $to; 
     mail($to,$subject,$message,$headers); 
     mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
     echo "Mail Sent. Thank you " . $fullName . ", we will contact you shortly."; 
     // You can also use header('Location: thank_you.php'); to redirect to another page. 
    } 
?> 


<form method="post" action="contact.php"> 
    <div class="form-group"> 

     <label for="fullName">Name</label> 
     <input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe"> 

     <label for="emailAddress">Email</label> 
     <input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="[email protected]"> 

     <label for="comment">Comment</label> 
     <textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea> 

     <button name="submit" type="submit" class="btn">Submit</button> 

    </div> 
</form> 

非常感谢!

+0

你得到了什么错误?代码是否进入条件?你可以用硬编码的值成功地运行单线邮件()调用吗? – mkaatman

+1

在本地服务器上,PHP邮件功能不起作用,但您可以使用PhpMailer(https://github.com/PHPMailer/PHPMailer)。 –

+0

我在代码中看不到任何错误。你得到什么错误?也作为@mkaatman moentioned,你可以用简单的邮件()函数在不同的页面上发送电子邮件吗? –

回答

0

在第二邮件功能的代码参数不会完成你没有定义subject2的价值,我认为你的第一条消息以正确的方式发送,但第二个将不

1

我会建议使用PHPMailer的。它是GitHub上的一个开源项目:PHPMailer - GitHub

该类允许您利用最着名的邮件平台的SMTP服务来获得使用PHP发送邮件的快速系统。
这是非常简单的建立一个代码与此类,从HTML表单开始:

<!DOCTYPE html> 
<html> 
<body> 
<form method="post"> 
<input type="email" name="from" placeholder="From"> 
<input type="email" name="to" placeholder="To"> 
<input type="text" name="subject" placeholder="Subject"> 
<textearea name="content"></textarea> 
</form> 
</body> 
</html> 

<?php 
require_once('PHPMailer_5.2.4\class.phpmailer.php'); 

if(isset($_POST["submit"])){ 
$username = 'YOUR_GMAIL_ACCOUNT'; 
$password = 'YOUR_ACCOUNT_PASSWORD'; 
$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 465; 
//$mail->addAttachment($_FILES["image"]["name"]); 
$mail->IsHTML(true); 
$mail->Username = $username; 
$mail->Password = $password; 
$mail->SetFrom($_POST["from"]); 
$mail->Subject = $_POST["subject"]; 
$mail->Body = $_POST["content"]; 
$mail->AddAddress($to); 

if(!$mail->Send()) 
{ 
    echo "Mailer error : " . $mail->ErrorInfo . "<br>"; 
} 
} 
?> 

正如你可以在PHP代码中看到的,我使用Gmail SMTP服务发送此邮件。请注意,如果您想使用其他服务,则必须更改SMTP服务器。
此外,您需要登录到您的电子邮件服务才能访问SMTP服务,而且您经常需要通过第三方应用程序访问您的邮件帐户。
在某些情况下,SMTP服务器将不接受TLS或SSL加密。


是否可以添加附件添加 enctype="multipart/data"属性到您的 form标记并通过 $_FILES阵列获取上传的文件。
我希望这会帮助你!