HTML / PHP从表单发送电子邮件[重复]

This question already has an answer here:

I've been working on this for some time and am quite new to php. I'm having trouble getting this to send. A second set of eyes on this code would be most helpful:

<?php
    if(isset($_POST['submit'])){
        $to = "myEmail"; // this is your Email address
        $from = $_POST['emailAddress']; // this is the sender's Email address
        $fullName = $_POST['fullName'];
        $subject = "Form submission";
        $message = $fullName . " wrote the following:" . "

" . $_POST['comment'];
        $message2 = "Here is a copy of your message " . $fullName . "

" . $_POST['comment'];

        $headers = "From:" . $from;
        $headers2 = "From:" . $to;
        mail($to,$subject,$message,$headers);
        mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
        echo "Mail Sent. Thank you " . $fullName . ", we will contact you shortly.";
        // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>


<form method="post" action="contact.php">
    <div class="form-group">

        <label for="fullName">Name</label>
        <input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe">

        <label for="emailAddress">Email</label>
        <input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="jane.doe@example.com">

        <label for="comment">Comment</label>
        <textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea>

        <button name="submit" type="submit" class="btn">Submit</button>

    </div>
</form>

Many thanks!

</div>

In your code parameters of second mail function are not completed you didn't define value of subject2 I think your first message send in the right way but the second will not

I would suggest to use PHPMailer. It is an open-source project available on GitHub : PHPMailer - GitHub

This class permits you to exploit the SMTP services of the most famous mailing platform to get a fast system to send mail using PHP.
It's really simple to set up a code with this class, starting from an HTML form :

<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="email" name="from" placeholder="From">
<input type="email" name="to" placeholder="To">
<input type="text" name="subject" placeholder="Subject">
<textearea name="content"></textarea>
</form>
</body>
</html>

<?php
require_once('PHPMailer_5.2.4\class.phpmailer.php');

if(isset($_POST["submit"])){
$username = 'YOUR_GMAIL_ACCOUNT';
$password = 'YOUR_ACCOUNT_PASSWORD';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//$mail->addAttachment($_FILES["image"]["name"]); 
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["from"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
$mail->AddAddress($to);

if(!$mail->Send())
{
    echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}
}
?>

As you can see in the PHP code, I'm using Gmail SMTP service to send this mail. Note that if you want to use other services you have to change the SMTP server.
Furthermore, you need to login into your email service to get an access to the SMTP service, and you need really often to enable the possibility of accessing your mailing account by third part applications too.
In some cases, the SMTP server won't accept TLS or SSL encryption.


Is it possible to add attachment adding enctype="multipart/data" attribute to your form tag and getting the uploaded file through the $_FILES array.
I hope this is going to help you!