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

时间:2013-05-04 05:39:12

标签: php email phpmailer

mail()函数无法在我的服务器上运行。我已经使用了基本的mail()代码来了解它是否存在脚本问题。它仍然没有发送电子邮件。有人建议我更改服务器上的设置以启用mail()函数或类似的东西。

我该怎么做?我怎么知道我的服务器允许mail()或它正确运行mail()?

有什么建议吗?

4 个答案:

答案 0 :(得分:0)

如果您在共享服务器上,我建议您与他们联系,如果他们负责。如果它发送电子邮件之前它可能是您可能导致的,您可能需要更改您的代码。 你没有提供任何示例代码,但这是我的,试试看:

    $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);  

答案 1 :(得分:0)

您在运行什么操作系统?

在ubuntu上,您可能会尝试安装邮件服务器(postfix或sendmail)。

apt-get install postfix

答案 2 :(得分:0)

如果您使用phpmailer库,则无法使用mail()。因为它有预定义的功能。您可以访问PhpMailer Example Page

来查看
  

PhpMailer使用$mail->Send()代替mail()

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!";
}

答案 3 :(得分:0)

在您的托管上进行测试,如果它没有工作您的托管已禁用邮件。哪些大多数免费服务都会阻止。给我发消息,为我的测试提供免费的小主机。

$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);