PHP mail function doesn't complete sending of

2019-09-06 06:36发布

<?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-06 07:06

Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled

require_once 'PHPMailer/PHPMailer.php';
require_once '/servicios/PHPMailer/SMTP.php';
require_once '/servicios/PHPMailer/Exception.php';

$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
try {
       //Server settings
       $mail->SMTPDebug = 0;                                 
       $mail->isSMTP();                                      
       $mail->Host = 'smtp.gmail.com';  
       $mail->SMTPAuth = true;                               
       $mail->Username = 'correo@gmail.com';                 
       $mail->Password = 'contrasenia';                           
       $mail->SMTPSecure = 'ssl';                          
       $mail->Port = 465;                                    

       //Recipients
       $mail->setFrom('correo@gmail.com', 'my name');    
       $mail->addAddress('destination@correo.com');               

       //Attachments
       $mail->addAttachment('optional file');         // Add files, is optional

       //Content
       $mail->isHTML(true);// Set email format to HTML
       $mail->Subject = utf8_decode("subject");
       $mail->Body    = utf8_decode("mail content");
       $mail->AltBody = '';
       $mail->send();
     }
     catch (Exception $e){
        $error = $mail->ErrorInfo;
     }
查看更多
【Aperson】
3楼-- · 2019-09-06 07:06

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 www.AceITLab.com, 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.

查看更多
聊天终结者
4楼-- · 2019-09-06 07:07

I think this should do the trick. I just added an if(isset and added concatenation to the variables in the body to separate PHP from HTML.

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

if (isset($_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>'; 
    }
}

?>
查看更多
聊天终结者
5楼-- · 2019-09-06 07:09

Try these two thigs separately and together:

  1. remove the if($_POST['submit']){}
  2. remove $from (just my gut)
查看更多
地球回转人心会变
6楼-- · 2019-09-06 07:11

If you only use the mail()function, you need to complete the config file.

You need to open the mail expansion, and set the SMTP smtp_port and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.

查看更多
贼婆χ
7楼-- · 2019-09-06 07:11

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.

查看更多
登录 后发表回答