简体   繁体   English

内联PHP发送邮件不起作用

[英]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. PHP不是我的强项,当我被要求实施模式新闻通讯订阅弹出窗口时,我感到有些畏缩,因为我知道我将不得不使用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. 我确信这确实是最基本的,但是由于缺乏PHP经验,我完全弄错了我要去哪里。 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. 我将文件另存为sendmail.php并上传到我的服务器。 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. 当我填写表格时,页面只是重新加载,它没有重定向到指定的URL,并且我没有收到任何电子邮件。 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. 编辑:将name属性添加到我的代码中,如下所示。 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. 您正在检查$_POST['submit']是否真实,但是您的表单从未设置该值。 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. 为了使submit密钥出现在您的请求中,您需要将其分配给表单中的字段。 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: 您可能希望将其添加到提交按钮,只需将name参数添加到您的提交按钮即可:

     <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. 即使进行了上述更改,由于$_POST['submit']的值为空,因此它仍将评估为false。 (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. 另外,您可以查找其他字段,例如nameemail ,因为您知道这两个字段都将在提交时设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM