2012-01-10 29 views
31

Tôi mới sử dụng PHP và tôi đang sử dụng chức năng thư để gửi email không hoạt động. Tôi nhận được một tin nhắn thành công, nhưng vẫn còn nó không hoạt độngThư PHP không hoạt động vì một số lý do

cùng một mã

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

Tôi có thiếu bất cứ điều gì, tôi cần phải bao gồm bất kỳ tập tin cho chức năng này .. Tôi đến từ asp.net & đây là tập lệnh cơ bản được tìm thấy trên trang web.

tôi đã cố gắng kịch bản khác liên quan đến thư họ không làm việc, hoặc ..

Tôi đang chạy script này trên trang web không phải trên localhost

+0

kiểm tra cấu hình của bạn –

+0

Nơi nào bạn sử dụng? Trên máy cục bộ hoặc trên máy chủ? – zozo

+0

Cấu hình – Learning

Trả lời

9

Đây có lẽ là một lỗi cấu hình. Nếu bạn nhấn mạnh vào việc sử dụng hàm PHP mail, bạn sẽ phải chỉnh sửa php.ini.

Nếu bạn đang tìm kiếm một tùy chọn dễ dàng hơn và linh hoạt hơn (theo ý kiến ​​của tôi), bạn nên sử dụng PHPMailer.

1

Kiểm tra cài đặt SMTP của bạn trong tệp php.ini. Máy chủ của bạn phải có một số tài liệu về thông tin đăng nhập nào cần sử dụng. Có lẽ bạn có thể kiểm tra tệp nhật ký lỗi của mình, tệp có thể có nhiều thông tin hơn.

2

Chức năng thư không đảm bảo việc gửi thư thực tế. Tất cả những gì bạn làm là chuyển thông điệp tới chương trình bên ngoài (thường là sendmail). Bạn cần một máy chủ SMTP được cấu hình đúng cách để làm việc này. Cũng nên nhớ rằng nó không hỗ trợ xác thực SMTP. Bạn có thể kiểm tra thư viện PEAR :: Mail của SwiftMailer, cả hai đều cung cấp cho bạn nhiều tùy chọn hơn.

5

Đây có thể là vấn đề của cấu hình SMTP trong tệp php.ini của bạn.

Vì bạn mới để PHP, Bạn có thể tìm thấy tập tin php.ini trong thư mục gốc của web site của thư mục cài đặt PHP và kiểm tra SMTP = và SMTP_PORT = và thay đổi giá trị

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

Trong trường hợp máy chủ của bạn yêu cầu xác thực để gửi thư, sử dụng chức năng thư PEAR.

59

Nếu bạn đang sử dụng Ubuntu và nó dường như sendmail không có trong /usr/sbin/sendmail, cài đặt sendmail bằng cách sử dụng thiết bị đầu cuối với lệnh này:

sudo apt-get install sendmail 

và sau đó chạy lại trang PHP nơi mail() được viết. Đồng thời kiểm tra thư mục spam của bạn.

+2

Cố định cho tôi, cảm ơn bạn. Đã bị mắc kẹt trên này trong nhiều giờ .. như một sửa chữa đơn giản lol - Chỉ cần thêm, tôi đang sử dụng một VPS 'ovh'. Vì vậy, nếu có ai trong tương lai nhìn thấy điều này, xin chào :) –

0

Đối với HostGator, bạn cần phải sử dụng sau cho tiêu đề của bạn:

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

Nó chỉ làm việc cho tôi khi từ người dùng là máy chủ e-mail trong khi Reply-To có thể là cái gì đó ví dụ khác nhau Từ: tên miền @ owner.com, Trả lời-To: [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

Tôi đang sử dụng này trong một thời bây giờ, không biết nếu điều này vẫn được cập nhật với các phiên bản PHP thực tế. Bạn có thể sử dụng tính năng này trong một thiết lập tệp hoặc chỉ chia nhỏ nó thành hai tệp như contact.php và index.php

contact.php | Mã

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

stylesheet

} 
.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 | MẪU MÃ

<?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> 
Các vấn đề liên quan