PHP邮件因某种原因不工作
我是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 &这是在网站上找到的基本脚本。
我试图邮寄他们没有工作,要么..
我在本地主机
运行此脚本ON THE网站不是这可能是一个配置错误相关的其他脚本。如果你坚持使用PHP mail
功能,你将不得不编辑php.ini
。
如果您正在寻找一个更简单,更通用的选项(在我看来),您应该使用PHPMailer。
检查您的SMTP设置在您的php.ini文件中。您的主机应该有一些关于使用什么凭证的文档。也许你可以检查你的错误日志文件,它可能有更多的信息可用。
邮件功能不保证邮件的实际传送。它所做的只是将消息传递给外部程序(通常是sendmail)。您需要配置正确的SMTP服务器才能正常工作。另外请记住它不支持SMTP认证。你可以看看SwiftMailer的PEAR :: Mail库,它们都给你更多的选择。
“仅仅因为你发送一封电子邮件,并不意味着它会到达。”
发送邮件严重的企业 - 例如您用作“发件人:”地址的域名可能会被配置为拒绝来自您的网络服务器的电子邮件。有关更长的概述(以及一些提示要检查的内容),请参阅http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html
这可能是您的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邮件功能。
如果你正在使用Ubuntu和它似乎sendmail的不/usr/sbin/sendmail
,安装sendmail的使用终端用这个命令:
sudo apt-get install sendmail
,然后运行其中重装mail()
写PHP页面。同时检查你的垃圾邮件文件
固定给我,谢谢。被困在这个几个小时..这样一个简单的修复大声笑 - 只是添加,我使用了一个'ovh' VPS。所以如果将来有人看到这个,你好:) – 2016-06-19 02:34:42
对于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
我现在用的这一段时间,不知道这是否仍然是最新的实际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 |表单代码
<?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>
检查配置 – 2012-01-10 13:28:40
你在哪里使用?在本地机器上还是在服务器上? – zozo 2012-01-10 13:29:03
什么配置 – Learning 2012-01-10 13:29:14