PHP 邮件由于某种原因不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8803994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mail not working for some reason
提问by Learning
I am new to PHP and I'm using the mail function to send emails which is not working. I get a success message, but still it does not work
我是 PHP 新手,我正在使用邮件功能发送不起作用的电子邮件。我收到一条成功消息,但仍然无法正常工作
same code
相同的代码
<?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.";
}
?>
Am I missing anything, do I need to include any files for this function.. I am from asp.net & this is the basic script which found on website.
我是否遗漏了任何东西,我是否需要为此功能包含任何文件..我来自 asp.net & 这是在网站上找到的基本脚本。
I tried other scripts related to mail they didn't work either..
我尝试了与邮件相关的其他脚本,但它们也不起作用。
I AM RUNNING THIS SCRIPT ON THE WEBSITE NOT on the localhost
我正在网站上而不是在本地主机上运行此脚本
采纳答案by Orentet
回答by Prabhat Kashyap
If you are using Ubuntu and it seem sendmail is not in /usr/sbin/sendmail
, install sendmail using the terminal with this command:
如果您使用的是 Ubuntu 并且似乎 sendmail 不在 中/usr/sbin/sendmail
,请使用终端使用以下命令安装 sendmail:
sudo apt-get install sendmail
and then run reload the PHP page where mail()
is written. Also check your spam folder.
然后运行重新加载mail()
写入的PHP页面。还要检查您的垃圾邮件文件夹。
回答by Robin Michael Poothurai
This might be the issue of your SMTP config in your php.ini file.
这可能是 php.ini 文件中 SMTP 配置的问题。
Since you new to PHP, You can find php.ini file in your root directory of PHP installation folder and check for SMTP = and smtp_port= and change the value to
由于您是 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)
In case your server require authentication for sending mail, use PEAR mail function.
如果您的服务器需要验证发送邮件,请使用 PEAR 邮件功能。
回答by Piskvor left the building
"Just because you send an email doesn't mean it will arrive."
“仅仅因为你发送了一封电子邮件并不意味着它会到达。”
Sending mail is Serious Business - e.g. the domain you're using as your "From:" address may be configured to reject e-mails from your webserver. For a longer overview (and some tips what to check), see http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html
发送邮件是一件严肃的事情——例如,您用作“发件人:”地址的域可能被配置为拒绝来自您的网络服务器的电子邮件。有关更长的概述(以及要检查的一些提示),请参阅http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html
回答by Maxim Krizhanovsky
The mail function do not guarantee the actual delivery of mail. All it do is to pass the message to external program (usually sendmail). You need a properly configured SMTP server in order for this to work. Also keep in mind it does not support SMTP authentication. You may check out the PEAR::Mail library of SwiftMailer, both of them give you more options.
邮件功能不保证邮件的实际送达。它所做的只是将消息传递给外部程序(通常是 sendmail)。您需要一个正确配置的 SMTP 服务器才能使其工作。另请记住,它不支持 SMTP 身份验证。您可以查看 SwiftMailer 的 PEAR::Mail 库,它们都为您提供了更多选择。
回答by TJHeuvel
Check your SMTP settings in your php.ini file. Your host should have some documentation about what credentials to use. Perhaps you can check your error log file, it might have more information available.
检查 php.ini 文件中的 SMTP 设置。您的主机应该有一些关于使用什么凭据的文档。也许您可以检查您的错误日志文件,它可能有更多可用信息。
回答by kztd
For HostGator, you need to use the following for your headers:
对于 HostGator,您需要对标头使用以下内容:
$headers = 'From: [email protected]' . " " .
'Reply-To: [email protected]' . " " .
'X-Mailer: PHP/' . phpversion();
It only worked for me when the from user was host e-mail while the Reply-To can be something different e.g. From: [email protected], Reply-To: [email protected]
它仅在发件人用户是主机电子邮件时才对我有用,而回复人可以是不同的东西,例如发件人:[email protected],回复人:[email protected]
http://support.hostgator.com/articles/specialized-help/technical/php-email-from-headerhttp://support.hostgator.com/articles/specialized-help/technical/how-to-use-sendmail-with-php
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
回答by HumanCK
I'm using this for a while now, don't know if this is still up to date with the actual PHP versions. You can use this in a one file setup, or just split it up in two files like contact.php and index.php
我现在用了一段时间,不知道这是否仍然与实际的 PHP 版本保持同步。您可以在一个文件设置中使用它,也可以将其拆分为两个文件,例如 contact.php 和 index.php
contact.php | Code
联系方式.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;
}
}
?>
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 | FORM CODE
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-\.]+@([\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>