简体   繁体   中英

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

mail() function is not working on my server. I have used basic mail() code to know whether its the problem of script. Still it didn't send email. Somebody advised me to change the setting on my server to enable mail() function or something like that.

How can I do that? How can I know that my server allows mail() or it runs mail() properly?

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).

apt-get install postfix

If you using phpmailer Library, you cant use mail() . Because it has predefined function. You can check that by visiting PhpMailer Example Page .

PhpMailer use $mail->Send() instead of mail()

Sample code of 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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