2012-01-10 29 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から午前..私はこの機能のために任意のファイルをインクルードする必要があります、私は何をしないのです&これはウェブサイトにある基本的なスクリプトです。

私はこれはおそらく、設定エラーでlocalhostの

+0

は、あなたがそれを使うのですか、あなたの設定 –

+0

をチェック!ローカルマシン上またはサーバー上 – zozo

+0

どのような設定 – Learning

答えて

9

にウェブサイト上でこのスクリプトを実行していないで...

を、彼らはどちらか動作しませんでしたメールに関連する他のスクリプトを試してみました。 PHP mailの使用を主張する場合は、php.iniを編集する必要があります。

私の意見では、より簡単で多目的なオプションをお探しの場合はPHPMailerを使用してください。

1

php.iniファイルのSMTP設定を確認してください。あなたのホストには、使用する資格情報に関するドキュメントが必要です。恐らく、エラーログファイルを確認することができます。利用可能な情報がもっとあるかもしれません。

2

メール機能は実際のメール配信を保証しません。外部プログラム(通常はsendmail)にメッセージを渡すだけです。これを行うには、適切に構成されたSMTPサーバーが必要です。また、SMTP認証をサポートしていないことに注意してください。あなたはSwiftMailerのPEAR :: Mailライブラリをチェックアウトすることができます。両方とももっと多くのオプションを提供します。

2

は、あなたが電子メールを送信するからといって、それが到着するという意味ではありません。」

送信メールは深刻なビジネスである - 例えば「送信者:」アドレスとして使用しているドメインが、Webサーバーからの電子メールを拒否するように構成されている可能性があります。より長い概観(そして何をチェックするべきか)については、http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html

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のページをリロードして実行します。Ubuntuを使用していて、sendmailは/usr/sbin/sendmailではないように見える場合

59

は、このコマンドを使用して、端末を使用してsendmailをインストールします。また、迷惑メールフォルダを確認してください。

+2

私のために修正しました、ありがとう。このような簡単な修正プログラムを追加しました。追加するだけで、私は 'ovh' VPSを使用しています。将来誰かがこれを見たら、こんにちは:) –

0

Hostgatorのために、あなたがあなたのヘッダに以下を使用する必要があります。

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

返信先は、例えば異なるものになることができますが、ユーザからホスト電子メールだったときにそれは私だけのために働いていました投稿者:owner @ domain。COMは、返信先[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 contact.phpとindex.phpのような2つのファイルにそれを分割することができますコード

<?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 |フォームコード

<?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> 
関連する問題