简体   繁体   中英

Inline PHP Send Mail not working

PHP isn't my strength and when I was asked to implement a modal newsletter subscription popup I cringed because I knew I was going to have to use PHP. I have scoured the internet all day trying various things, but I cannot seem to get anything to work and I am really hoping that someone will be able to point me in the right direction. It is super simple, I only need a name and email address which is then sent to an account I specify. I am sure this is really basic, but with my lack of experience with PHP I am totally lost as to where I am going wrong. My code is below.

<?php
if($_POST["submit"]) {
    $recipient="my@emailaddress.com";
    $subject="Newsletter Subscription";
    $senderName=$_POST["name"];
    $senderEmail=$_POST["email"];

    $mailBody="Name: $senderName\nEmail: $senderEmail\n\nThis is a test!";

    mail($recipient, $subject, $mailBody, "From: $senderName <$senderEmail>");

    header('Location:http://www.urltoredirect.com/');
}
?>

<html>

<head>
    <title></title>
</head>

<body>
    <form method="post" action="sendmail.php">
        <div class="rbm_input_txt">
            <input type="name" name="name" placeholder="name" required>
        </div>
        <div class="rbm_input_txt">
            <input type="email" name="email" placeholder="email" required>
        </div>
        <div class="rbm_form_submit">
            <button type="submit" class="rbm_btn_x_out_shtr">subscribe</button>
        </div>
    </form>
</body>

</html>

I save the file as sendmail.php and upload to my server. When I fill out the form, the page just reloads, it isn't redirecting to the URL specified and I am not receiving any email. I suspect the issue is in the send mail and once that is sorted, the redirect will work. Can anyone see anything obvious that I am missing?

EDIT: Added the name attribute to my code as suggested below. Issue is still unresolved.

标签中必须有名称

  <div class="rbm_input_txt"><input type="name" placeholder="name" name="name" required><input type="email" placeholder="email" name="email``" required

You're checking to see if $_POST['submit'] is truthy, but your form never actually sets that value. There are two changes you can make to fix this:

  1. In order for the submit key to be present in your request, you need to assign it to a field in your form. You'll likely want to add it to the submit button, which can be done by simply adding the name parameter to your submit button:

     <button name="submit" type="submit" class="rbm_btn_x_out_shtr">subscribe</button> 
  2. Even with the above change, $_POST['submit'] will still evaluate to false since its value will be empty. (You can see this in the rules for boolean conversions .) You can get around this by checking to see if its set, rather than if it's truthy:

     if (isset($_POST['submit'])) 

Alternatively, you could just look for a different field such as name or email , since you know both of those fields will be set on submit.

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