简体   繁体   中英

my php mail function doesn't seem to be sending me an email

My email function isn't working on my website. When I complete the form it doesn't send to my email address? What am I missing? Can anyone help steer me in the right direction?

<?php 
                // form submitted
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {

                // list variables
                $name = $_POST['name'];
                $email = $_POST['email'];
                $comment = $_POST['comment'];
                $company = $_POST['company'];
                $industry = $_POST['industry'];
                $phone = $_POST['phone'];

                // email
                $to = 'myemail@example.com';
                $subject = 'Contact Form:';
                $emailBody = "
                Hi,
                <br /><br />
                <strong>Name</strong> : $name.<br />
                <strong>Phone</strong> : $phone.<br />
                <strong>Email</strong> : $email.<br />
                <strong>Company</strong> : $company.<br />
                <strong>Industry</strong> : $Industry.<br />
                <strong>Comments</strong> : $comment.";




                // mail

                if ($_POST['name']) {
                    } else {
                        echo "please specify your name<br>";
                    }


                if ($_POST['email']) {
                    } else {
                        echo "please specify your email<br>";
                    }

                if ($_POST['comment']) {
                    echo "Hi, $name, <br>
                    Thank you for the following comments: <br> <em>$comment</em>
                    <br>I will get back to you shortly at $email";
                    } else {
                        echo 'please specify your request';
                    }
                }

                if ($name && $email && $comments) {
                    mail('$to', '$subject', 'From: $emailBody'); 
                }  

                ?>

Many thanks for your help

Basic PHP: You're using the WRONG string types:

mail('$to', '$subject', 'From: $emailBody'); 
     ^---^---

' -quoted strings do NOT expand variables. You're trying to send the email to an address which is the literal characters $ , t , and o . For such "just a variable" usage, you won't need quotes AT ALL:

mail($to, $subject, "From: $emailBody");
                    ^---             ^--- note the different quotes here

To add to Marc's answer: ( compliment, I should say )

This if ($name && $email && $comments) should be if ($name && $email && $comment) since you have $comment = $_POST['comment']; therefore no variable match, the conditional statement fails because of it, and so will the mail() function.

Change to:

if ($name && $email && $comment) {
    mail($to, $subject, "From: $emailBody");
}  

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