简体   繁体   English

mail()函数无法正常工作 - 如何解决?

[英]mail() function not working - How to resolve it?

mail() function is not working on my server. mail()函数在我的服务器上不起作用。 I have used basic mail() code to know whether its the problem of script. 我已经使用了基本的mail()代码来了解它是否存在脚本问题。 Still it didn't send email. 它仍然没有发送电子邮件。 Somebody advised me to change the setting on my server to enable mail() function or something like that. 有人建议我更改服务器上的设置以启用mail()函数或类似的东西。

How can I do that? 我怎样才能做到这一点? How can I know that my server allows mail() or it runs mail() properly? 我怎么知道我的服务器允许mail()或它正确运行mail()?

Any advice? 有什么建议?

If you are on a shared server fisrt i advise you to contact with them if they are responsible for it. 如果您在共享服务器上,我建议您与他们联系,如果他们负责。 if it was sending emails before it could be something you could cause and you may need to change your code. 如果它发送电子邮件之前它可能是您可能导致的,您可能需要更改您的代码。 you did not provide any sample code but here is mine, try it: 你没有提供任何示例代码,但这是我的,试试看:

    $to= "$confoemail";
    $subject="Your Contact Request at somewebsite.Com";
    $message= "the message to send";
    $headers  = 'MIME-Version: 1.0' . "\r\n".
     'Content-type: text/html; charset=iso-8859-1' . "\r\n".
     'From: justin@webmasteroutlet.com' . "\r\n" . //that code here //perfectly works, search if the code is built -in of php.
   'Reply-To: justin@webmasteroutlet.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
    mail($to,$subject, $message, $headers);  

What operating system are you running? 你在运行什么操作系统?

On ubuntu, you might try to install a mail server (postfix or sendmail). 在ubuntu上,您可能会尝试安装邮件服务器(postfix或sendmail)。

apt-get install postfix

If you using phpmailer Library, you cant use mail() . 如果您使用phpmailer库,则无法使用mail() Because it has predefined function. 因为它有预定义的功能。 You can check that by visiting PhpMailer Example Page . 您可以访问PhpMailer示例页面来检查。

PhpMailer use $mail->Send() instead of mail() PhpMailer使用$mail->Send()而不是mail()

Sample code of PhpMailer PhpMailer的示例代码

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

Test this on your hosting, if it doesn't work your hosting has mail disabled. 在你的主机上测试这个,如果它不起作用你的主机已禁用邮件。 which most free services do block. 哪些大多数免费服务都会阻止。 message me for a free small hosting for ur tests. 给我发消息,为我的测试提供免费的小主机。

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$name = "somename"; $email="test@test.com"; $phone="1111111111" $subject="test"; $message="the message";

$to      = 'info@fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info@fullertoncomputerepairwebdesign.com' . "\r\n" .
    'Reply-To: info@fullertoncomputerepairwebdesign.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
                $phone."</br>Subject: ".$subject.
                "</br>Message: ".$message;
//echo $themessage;
mail($to, $subject, $themessage, $headers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM