简体   繁体   English

在表单提交期间发送电子邮件

[英]Send an email during form submit

Sorry if the title is a bit vague. 对不起,如果标题有点模糊。 What I mean to say is I have a code like below: 我的意思是我有一个如下代码:

<?php 
    /* ...generate $to, $subject, $msg, $from variables here */

    if(isset($_POST['ccbutton_pressed'])) {
    if(mail($to, $subject, $msg, $from)){
        echo 'Order Sent';
    }else{
        echo 'Order Sending Error';
    }
}?>

<form method='post' action='SOME_URL_TO_POST_TO' >
    <!-- some visible input types -->
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <!-- some hidden input types -->
    <input type='hidden' name='hiddenfield1' value='1' />
    <input type='hidden' name='hiddenfield2' value='2' />
    <input type='submit' value='Submit' />
    <input type='hidden' name='ccbutton_pressed' value='1'>
</form>

Is there some way I can send an email upon submission? 有什么方法可以在提交时发送电子邮件吗? While taking notes on the following points below: 同时记下以下几点:

  1. prefer to php because it is server side and information is kept private 喜欢php,因为它是服务器端,信息保密
  2. 'action' attribute needs to post and may not be changed (I need it to go to another page/different form) 'action'属性需要发布,可能不会更改(我需要它去另一个页面/不同的表单)
  3. Open to using Javascript as long as the contents of the email can be hidden somehow (but doubtful) 只要电子邮件的内容可以以某种方式隐藏(但可疑),可以使用Javascript打开
  4. I already tried putting a php block that triggered when a hidden field got set in that form but it did not work in this case. 我已经尝试过放置一个在我的表单中设置隐藏字段时触发的php块,但在这种情况下它不起作用。

Update: Changed code block to explain more information. 更新:更改了代码块以解释更多信息。 This is also not a duplicate because in Send email with PHP from html form on submit with the same script , the 'action' is left blank while I need mine to post to somewhere. 这也不是重复,因为在使用相同脚本提交时使用html表单发送带有PHP的电子邮件时 ,'action'留空,而我需要将其发布到某个地方。

I'd also like to add that I have checked the above article and it doesn't work for me. 我还想补充一点,我已经检查了上面的文章,但这对我不起作用。

<?php 

if(isset($_POST['submit'])){
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $toemail="abc@gmail.com";
    $subject="Sending Mail";
    $message="Firstname:".$fname."<br/>";
    $message.="Lastname:".$lname;
    $headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
    //sending simple mail
    mail($toemail, $subject, $message, $header);
}
?>

<form method='post' action='' >
    <!-- some visible input types -->
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <!-- some hidden input types -->
    <input type='hidden' name='hiddenfield1' value='1' />
    <input type='hidden' name='hiddenfield2' value='2' />
    <input type='submit' value='Submit' id="submit" name="submit" />
</form>

Cheers 干杯

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

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