2012-01-10 6 views
31

저는 PHP를 처음 사용하고 있으며 메일 기능을 사용하여 작동하지 않는 이메일을 보내고 있습니다. 나는 성공 메시지를 얻을 수 있지만, 여전히이PHP 메일이 어떤 이유로 작동하지 않습니다.

같은 코드가 작동하지 않습니다

<?php 
    $email_to = "[email protected]"; 
    $email_subject = "Test mail"; 
    $email_body = "Hello! This is a simple email message."; 


    if(mail($email_to, $email_subject, $email_body)){ 
     echo "The email($email_subject) was successfully sent."; 
    } else { 
     echo "The email($email_subject) was NOT sent."; 
    } 
?> 

내가 asp.net에서 나는 .. 나는이 기능에 대한 모든 파일을 포함해야합니까, 난 아무것도없는 건가요 & 이것은 웹 사이트에서 발견 된 기본 스크립트입니다.

나는 그들이이 아마 구성 오류가 나는 로컬 호스트

+0

는 구성 –

+0

을 확인? 로컬 컴퓨터 또는 서버에 있습니까? – zozo

+0

어떤 구성 – Learning

답변

9

의 웹 사이트에서이 스크립트를 NOT 실행하고 있습니다 ..

중 하나가 작동하지 않았다 메일과 관련된 다른 스크립트를 시도했다. PHP mail 기능을 사용하고 싶다면 php.ini을 편집해야합니다.

내 생각에 더 쉽고 다양한 옵션을 찾고 있다면 PHPMailer을 사용해야합니다.

1

php.ini 파일에서 SMTP 설정을 확인하십시오. 호스트는 사용할 자격 증명에 대한 문서를 가지고 있어야합니다. 아마도 오류 로그 파일을 검사 할 수 있으며 더 많은 정보를 얻을 수 있습니다.

2

메일 기능은 메일의 실제 배달을 보장하지 않습니다. 메시지를 외부 프로그램 (보통 sendmail)에 전달하는 것뿐입니다. 이 기능을 사용하려면 올바르게 구성된 SMTP 서버가 필요합니다. 또한 SMTP 인증을 지원하지 않습니다. SwiftMailer의 PEAR :: Mail 라이브러리를 확인해보십시오. 둘 다 더 많은 옵션을 제공합니다.

5

을 참조하십시오. php.ini 파일에 SMTP 구성의 문제 일 수 있습니다.

당신이 PHP 새로운, 당신은 PHP 설치 폴더의 루트 디렉토리에있는 php.ini의 파일을 찾을 수 및 SMTP를 확인 = 및 SMTP_PORT = 및 서버가 요구하는 경우

SMTP = your mail server e.g) mail.yourdomain.com 
smtp_port = 25(check your admin for original port) 

에 값을 변경할 수 있기 때문에 메일을 보내는 인증, PEAR 메일 기능을 사용하십시오.

sudo apt-get install sendmail 

을 다음 mail()가 기록되는 PHP 페이지를 다시로드 실행 : 당신은 우분투를 사용하고 센드 메일이 /usr/sbin/sendmail에없는 보일 경우

59

,이 명령으로 터미널을 사용하여 sendmail을 설치합니다. 또한 스팸 폴더를 확인하십시오.

+2

고쳐 주셔서 고마워요. 이걸 몇 시간 동안 붙잡 혔어. 그런 간단한 수정 LOL - 그냥 추가하기 위해, 나는'ovh' VPS를 사용하고 있습니다. 그래서 미래에 누군가가 이것을 본다면,여보세요 :) –

0

는 HostGator에 들어, 당신은 당신의 헤더에 대해 다음을 사용할 필요가 :

$headers = 'From: [email protected]' . " " . 
'Reply-To: [email protected]' . " " . 
'X-Mailer: PHP/' . phpversion(); 

회신-에 뭔가 다른, 예를 들어 수 있지만 사용자의 호스트 이메일 때 그것은 단지 나를 위해 일한 보낸 사람 : owner @ domain.co.kr에서, 답장 : [email protected]

http://support.hostgator.com/articles/specialized-help/technical/php-email-from-header http://support.hostgator.com/articles/specialized-help/technical/how-to-use-sendmail-with-php

0

지금 잠시 동안 사용하고,이 실제 PHP 버전과 최신 아직 알고하지 않습니다. 이 파일을 하나의 파일 설정에서 사용하거나 contact.php와 index.php와 같은 두 파일로 나눌 수 있습니다.

contact.php | 코드

<?php 
error_reporting(E_ALL^E_NOTICE); 


if(isset($_POST['submitted'])) { 


if(trim($_POST['contactName']) === '') { 
    $nameError = '<span style="margin-left:40px;">You have missed your name.</span>'; 
    $hasError = true; 
} else { 
    $name = trim($_POST['contactName']); 
} 

if(trim($_POST['topic']) === '') { 
    $topicError = '<span style="margin-left:40px;">You have missed the topic.</span>'; 
    $hasError = true; 
} else { 
    $topic = trim($_POST['topic']); 
} 

$telefon = trim($_POST['phone']); 
$company = trim($_POST['company']); 


if(trim($_POST['email']) === '') { 
    $emailError = '<span style="margin-left:40px;">You have missed your email adress.</span>'; 
    $hasError = true; 
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) { 
    $emailError = '<span style="margin-left:40px;">You have missspelled your email adress.</span>'; 
    $hasError = true; 
} else { 
    $email = trim($_POST['email']); 
} 


if(trim($_POST['comments']) === '') { 
    $commentError = '<span style="margin-left:40px;">You have missed the comment section.</span>'; 
    $hasError = true; 
} else { 
    if(function_exists('stripslashes')) { 
     $comments = utf8_encode(stripslashes(trim($_POST['comments']))); 
    } else { 
     $comments = trim($_POST['comments']); 
    } 
} 


if(!isset($hasError)) { 

    $emailTo = '[email protected]'; 
    $subject = 'Example.com - '.$name.' - '.$betreff; 
    $sendCopy = trim($_POST['sendCopy']); 
    $body = "\n\n This is an email from http://www.example.com \n\nCompany : $company\n\nName : $name \n\nEmail-Adress : $email \n\nPhone-No.. : $phone \n\nTopic : $topic\n\nMessage of the sender: $comments\n\n"; 
    $headers = "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"; 

    mail($emailTo, $subject, $body, $headers); 



    $emailSent = true; 
} 
} 
?> 

스타일

} 
.formblock{display:block;padding:5px;margin:8px; margin-left:40px;} 
.text{width:500px;height:200px;padding:5px;margin-left:40px;} 
.center{min-height:12em;display:table-cell;vertical-align:middle;} 
.failed{ margin-left:20px;font-size:18px;color:#C00;} 
.okay{margin-left:20px;font-size:18px;color:#090;} 
.alert{border:2px #fc0;padding:8px;text-transform:uppercase;font-weight:bold;} 
.error{font-size:14px;color:#C00;} 

label 
{ 
margin-left:40px; 
} 

textarea 

{ 
margin-left:40px; 
} 

의 index.php | 당신이 사용합니까 어디 FORM 코드

<?php header('Content-Type: text/html;charset=UTF-8'); ?> 
<!DOCTYPE html> 
<html lang="de"> 
<head> 
<script type="text/javascript" src="js/jquery.js"></script> 
</head> 
<body> 


<form action="contact.php" method="post"> 

<?php if(isset($emailSent) && $emailSent == true) { ?> 

<span class="okay">Thank you for your interest. Your email has been send !</span> 

<br> 

<br> 

<?php } else { ?> 

<?php if(isset($hasError) || isset($captchaError)) { ?> 

<span class="failed">Email not been send. Please check the contact form.</span> 

<br> 

<br> 

<?php } ?> 

<label class="text label">Company</label> 

<br> 

<input type="text" size="30" name="company" id="company" value="<?php if(isset($_POST['company'])) echo $_POST['comnpany'];?>" class="formblock" placeholder="Your Company"> 

<label class="text label">Your Name <strong class="error">*</strong></label> 

<br> 

<?php if($nameError != '') { ?> 

<span class="error"><?php echo $nameError;?></span> 

<?php } ?> 

<input type="text" size="30" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="formblock" placeholder="Your Name"> 

<label class="text label">- Betreff - Anliegen - <strong class="error">*</strong></label> 

<br> 

<?php if($topicError != '') { ?> 

<span class="error"><?php echo $betrError;?></span> 

<?php } ?> 

<input type="text" size="30" name="topic" id="topic" value="<?php if(isset($_POST['topic'])) echo $_POST['topic'];?>" class="formblock" placeholder="Your Topic"> 

<label class="text label">Phone-No.</label> 

<br> 

<input type="text" size="30" name="phone" id="phone" value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>" class="formblock" placeholder="12345 678910"> 

<label class="text label">Email-Adress<strong class="error">*</strong></label> 

<br> 

<?php if($emailError != '') { ?> 

<span class="error"><?php echo $emailError;?></span> 

<?php } ?> 

<input type="text" size="30" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="formblock" placeholder="[email protected]"> 

<label class="text label">Your Message<strong class="error">*</strong></label> 

<br> 

<?php if($commentError != '') { ?> 

<span class="error"><?php echo $commentError;?></span> 

<?php } ?> 

<textarea name="comments" id="commentsText" class="formblock text" placeholder="Leave your message here..."><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> 

<button class="formblock" name="submit" type="submit">Send Email</button> 
<input type="hidden" name="submitted" id="submitted" value="true"> 
<?php } ?> 

</form> 
</body> 
</html> 

JAVASCRIPT

<script type="text/javascript"> 

<!--//--><![CDATA[//><!-- 

$(document).ready(function() { 

$('form#contact-us').submit(function() { 

$('form#contact-us .error').remove(); 
var hasError = false; 

$('.requiredField').each(function() { 

if($.trim($(this).val()) == '') { 
var labelText = $(this).prev('label').text(); 

$(this).parent().append('<br><br><span style="margin-left:20px;">You have missed '+labelText+'.</span>.'); 

$(this).addClass('inputError'); 
hasError = true; 

} else if($(this).hasClass('email')) { 

var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
if(!emailReg.test($.trim($(this).val()))) { 

var labelText = $(this).prev('label').text(); 

$(this).parent().append('<br><br><span style="margin-left:20px;">You have entered a wrong '+labelText+' adress.</span>.'); 

$(this).addClass('inputError'); 
hasError = true; 
} 
} 
}); 
if(!hasError) { 

var formInput = $(this).serialize(); 

$.post($(this).attr('action'),formInput, function(data){ 

$('form#contact-us').slideUp("fast", function() {     
$(this).before('<br><br><strong>Thank You!</strong>Your Email has been send successfuly.'); 

}); 

}); 

} 
return false; 

}); 

}); 

//-->!]]> 

</script> 
관련 문제