PHP mail function doesn't complete sending of

2019-09-10 03:35发布

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: yoursite.com'; 
    $to = 'contact@yoursite.com'; 
    $subject = 'Customer Inquiry';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
?>

I've tried creating a simple mail form. The form itself is on my index.html page, but submits to a separate "thank you for your submission" page, thankyou.php, where the above PHP code is embedded. The code submits perfectly, but never sends an email. please help.

标签: php html email
27条回答
疯言疯语
2楼-- · 2019-09-10 04:20

If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput.

In the case if you are using a free hosing service like 000webhost or hostinger, those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.

If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference, PHP mail()

To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)

<?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
?>

Hope this helped.

查看更多
何必那么认真
3楼-- · 2019-09-10 04:21

You can use config email by codeigniter, example using smtp (simple way) :

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'mail.domain.com', //your smtp host
        'smtp_port' => 26, //default port smtp
        'smtp_user' => 'name@domain.com',
        'smtp_pass' => 'password',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
);
$message = 'Your msg';
$this->load->library('email', $config);
$this->email->from('name@domain.com', 'Title');
$this->email->to('emaildestination@domain.com');
$this->email->subject('Header');
$this->email->message($message);

if($this->email->send()) 
{
   //conditional true
}

It's works for me!

查看更多
够拽才男人
4楼-- · 2019-09-10 04:23
  1. Always try sending headers in mail function.
  2. If you are sending mail through localhost then do the smtp settings for sending mail.
  3. If you are sending mail through server then check the email sending feature is enabled on your server.
查看更多
登录 后发表回答