2017-03-28 9 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

어떤 오류가 발생합니까? 코드가 조건부에 들어가 있습니까? 하드 코딩 된 값을 가진 한 줄의 mail() 호출을 성공적으로 실행할 수 있습니까? – mkaatman

+1

로컬 서버에서는 PHP 메일 기능이 작동하지 않지만 PhpMailer (https://github.com/PHPMailer/PHPMailer)를 사용할 수 있습니다. –

+0

코드에 아무 것도 표시되지 않습니다. 어떤 오류가 발생 했습니까? 또한 @mkaatman이 모인 상태에서 다른 페이지의 일반 메일() 기능으로 이메일을 보낼 수 있습니까? –

답변

0

는 당신이 subject2의 값을 정의하지 않았다 완료되지 않은 나는 당신의 첫 번째 메시지는 올바른 방법으로 보낼 수 있지만, 두 번째는하지

1

내가 PHPMailer를 사용하는 것이 좋습니다 것이다 것이라 생각합니다. 이것은 GitHub에서 사용할 수있는 오픈 소스 프로젝트입니다 : PHPMailer - GitHub

이 클래스는 가장 유명한 메일 플랫폼의 SMTP 서비스를 이용하여 PHP를 사용하여 메일을 보내는 빠른 시스템을 이용할 수있게합니다.
그것은 HTML 양식에서 시작,이 클래스와 코드를 설정하는 정말 간단합니다 : 당신이 PHP 코드에서 볼 수 있듯이, 나는이 메일을 보낼 Gmail의 SMTP 서비스를 사용하고

<!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>"; 
} 
} 
?> 

. 다른 서비스를 사용하려면 SMTP 서버를 변경해야합니다.
또한 SMTP 서비스에 액세스하려면 이메일 서비스에 로그인해야하며, 3 부 애플리케이션에서도 메일 계정에 액세스 할 수 있어야합니다.
경우에 따라 SMTP 서버는 TLS 또는 SSL 암호화를 허용하지 않습니다.


form 태그에 enctype="multipart/data" 속성을 추가하고 업로드 된 파일을 $_FILES 배열로 가져 오는 첨부 파일을 추가 할 수 있습니까?
나는 이것이 당신을 도울 것이라는 희망을 품는다!