PHP mail function doesn't complete sending of

2019-09-10 03:35发布

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From:'; 
    $to = ''; 
    $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
We Are One
2楼-- · 2019-09-10 03:56

This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.

If you're going to a university that runs XAMPP from, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.

Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.

3楼-- · 2019-09-10 04:01

Make sure you have Sendmail installed in your server.

If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.

If it is empty, you will need to do a:

sudo apt-get install sendmail

if you are on an Ubuntu server.

4楼-- · 2019-09-10 04:03
$name = $_POST['name'];
$email = $_POST['email'];
$reciver = '/* Reciver Email address */';
if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
    $subject = $name;
    // To send HTML mail, the Content-type header must be set.
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From:' . $email. "\r\n"; // Sender's Email
    //$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
    $template = '<div style="padding:50px; color:white;">Hello ,<br/>'
        . '<br/><br/>'
        . 'Name:' .$name.'<br/>'
        . 'Email:' .$email.'<br/>'
        . '<br/>'
        . '</div>';
    $sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
    // Message lines should not exceed 70 characters (PHP rule), so wrap it.
    $sendmessage = wordwrap($sendmessage, 70);
    // Send mail by PHP Mail Function.
    mail($reciver, $subject, $sendmessage, $headers);
    echo "Your Query has been received, We will contact you soon.";
} else {
    echo "<span>* invalid email *</span>";
5楼-- · 2019-09-10 04:03

You can use libmail:

include "libmail.php";
$m = new Mail(); // create the mail
$m->From( $_POST['form'] );
$m->To( $_POST['to'] );
$m->Subject( $_POST['subject'] );
$m->Body( $_POST['body'] );
$m->Cc( $_POST['cc']);
//  attach a file of type image/gif to be displayed in the message if possible
$m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
$m->Send(); // send the mail
echo "Mail was sent:"
echo $m->Get(); // show the mail source
6楼-- · 2019-09-10 04:05

First of all,

You might have to many parameters for the mail() function... You are able to have 5 max. mail(to,subject,message,headers,parameters); As far as the $from variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From:'; 
$to = ''; 
$subject = 'Customer Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

Also make sure you have the correct order of variables in your mail() function. the mail($to,$subject,$message,etc.) in that order, or else there is a chance of it not working. Let me know if this helps...

7楼-- · 2019-09-10 04:06

If you are using php mailer then you can use this code, just copy paste it, replace the *** with your data and credentials. And also change the value of input data name.


    require_once ("PHPMailer/PHPMailerAutoload.php");

    $error = array();


    $message = $_POST['a'] . '<br>';
    $message.= $_POST['b'] . '<br>';
    $message.= $_POST['c'] . '<br>';
    $message.= $_POST['d'] . '<br>';
    $message.= $_POST['e'] . '<br>';
    $message.= $_POST['f'] . '<br>';
    $message.= $_POST['g'] . '<br>';
    $message.= $_POST['h'] . '<br>';
    $message.= $_POST['i'] . '<br>';
    $message.= $_POST['j'] . '<br>';
    $message.= $_POST['k'] . '<br>';

    $subject = "Visitor Query";
    $from = "******@******.com"; // example
    $password = '******';

    $mail = new PHPMailer();
    $body = $message;
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->SMTPDebug = 0;

    //$mail->Host = 'mail.******.com'; // sets SMTP server
    $mail->Host = 'mail.******.com'; // sets SMTP server        
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    //$mail->Port = 25;

    $mail->Username = $from; // SMTP username
    $mail->Password = $password; // SMTP password

    $mail->AddAddress ( "******" );
    $mail->AddReplyTo ( "******", "Team");
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->From =  $from;
    $mail->FromName = "Visitor's Query";
    $mail->ContentType = "text/html";

    if (count($error == 0)) {


        // echo "_________________wait_______for_______ our _______ reply  !";

    } else {

        echo $error; // show error messages
        //echo $result;

    //header("location: index.php");

登录 后发表回答