如何发送邮件动态块的html代码? [英] how to email dynamic block of html code?
问题描述
你好,我想知道是否有任何方式发送一个HTML代码块到一个电子邮件地址。我有一个名为sum的外部javaScript文件中设置的变量,我在html代码中调用该变量,因此html块的一部分正在动态填充。我不知道如何通过电子邮件发送这个正常的php方式,所以我只是想知道我是否可以发送整个html代码块到一个电子邮件地址。
这里是整个块的HTML代码我想发送:
< div class =select-location-page>
< h2>我们应该在哪里见到你?< / h2>
< p class =location-sub-header>填写此表单,我们将在1小时内与您会面!< / p>
< form action =../ mail.phpmethod =POSTclass =location-form>
< div class =device-details>
< h2>装置:iPhone5< / h2>
< h2>修复费用:$< span id =result> 0< / span>< / h2>
< / div>
< input name =nametype =textid =nameplaceholder =Namemaxlength =20>
< input name =numbertype =textid =numberplaceholder =Numbermaxlength =15>
< input name =addresstype =textid =addressplaceholder =Addressmaxlength =40>
< input name =citytype =textid =cityplaceholder =Citymaxlength =20>
< input name =ziptype =textid =stateplaceholder =Zipmaxlength =20>
< input name =devicetype =hiddenid =devicevalue =iPhone5maxlength =20>
< div class =submit-btn>
< input type =submitvalue =提交请求/>
< / div>
< / form>
< script>
document.getElementById(result)。innerHTML = localStorage.getItem(sum);
< / script>
< / div>
这里是我试图创建的php文件,除了修复成本之外,它都工作在html代码中声明一个php变量,然后将其放在mail.php文件中,但它不起作用)
?php
$ name = $ _POST ['name'];
$ number = $ _POST ['number'];
$ address = $ _POST ['address'];
$ city = $ _POST ['city'];
$ zip = $ _POST ['zip'];
$ device = $ _POST ['device'];
$ formcontent =From:$ name \\\
number:$ number \\\
地址:$ address \\\
城市:$ city \\\
Zip $ zip \\\
设备:$ device \\\
Repair成本:(需要工作);
$ recipient =joe.t.oconnor@gmail.com;
$ subject =设备维修请求;
$ mailheader =From:$ name \r\\\
;
邮件($ recipient,$ subject,$ formcontent,$ mailheader)或死(错误!);
header('Location:thankyou.html');
?>
这是一个令人困惑的问题,任何帮助将不胜感激。谢谢!
(请参阅下面的编辑)
使用输出缓冲来捕获表单并使用完整的http呼叫来执行该操作并以HTML格式发送。
Sidenote:确保更改所有实例 example.com
到您的服务器地址。
示例:
<?php
ob_start();
echo'
< form action =http://www.example.com/mail.phpmethod =POSTclass =location-form >
< div class =device-details>
< h2>装置:iPhone5< / h2>
< h2>修复费用:$< span id =result> 0< / span>< / h2>
< / div>
< input name =nametype =textid =nameplaceholder =Namemaxlength =20>
< input name =numbertype =textid =numberplaceholder =Numbermaxlength =15>
< input name =addresstype =textid =addressplaceholder =Addressmaxlength =40>
< input name =citytype =textid =cityplaceholder =Citymaxlength =20>
< input name =ziptype =textid =stateplaceholder =Zipmaxlength =20>
< input name =devicetype =hiddenid =devicevalue =iPhone5maxlength =20>
< div class =submit-btn>
< input type =submitvalue =提交请求/>
< / div>
< / form>
';
$ output = ob_get_contents();
ob_end_clean();
$ to =email@example.com;
$ from =email@example.com;
$ subject =表单提交;
$ message = $ output;
$ headers ='MIME-Version:1.0'。 \r\\\
;
$ headers。='Content-type:text / html; charset = iso-8859-1'。 \r\\\
;
$ headers。=From:。从$;
mail($ to,$ subject,$ message,$ headers);
然后在您的 mail.php
文件中:
Sidenote:我省略了一些POST数组。你需要自己添加。这只是为我工作的一个例子。
<?php
$ first_name = $ _POST ['name'];
//您的表单中不存在以下内容。你可以适应你的喜好。
$ message = $ _POST ['message'];
$ to =email@example.com;
$ from =email@example.com;
$ subject =表单提交2;
$ headers ='MIME-Version:1.0'。 \r\\\
;
$ headers。='Content-type:text / html; charset = iso-8859-1'。 \r\\\
;
$ headers。=From:。从$;
mail($ to,$ subject,$ message,$ headers);
脚注: / p>
您可能无法在此处使用JS,但可以尝试一下。我没有在测试中使用它。
您还必须使用某种形式的按钮或链接来告诉人们点击发送。这是一个实验答案。我会尽快修改它。
参考文献:
- http://php.net/manual/en/function.ob-start。 php
- http ://php.net/manual/en/function.ob-get-contents.php
- http://php.net/manual/en/function.ob-end-clean.php
编辑:
,编辑)作品。 (您将需要将其他POST数组添加到 mail.php
,如注释所述)。
Sidenote:请参阅代码中的一些添加说明的注释。 //评论...
- 用户输入他们的电子邮件地址(取自一个单独的表单)。
- 如果它不是空的,在POST数组中捕获电子邮件。
- 如果电子邮件不为空,则捕获整个$ / $>
新的代码:
<?php
echo $ form ='
< ; form action =method =POSTclass =location-form>
< input name =email_fromtype =textid =email_from>
< input type =submitname =submitvalue =Submit Request/>
< / form>
';
如果(!empty($ _ POST ['email_from'])){
//输入邮件后,会向用户输入一条消息并提交。
echo $ from2 =电子邮件已发送到:。 $ _POST [ EMAIL_FROM];
}
$ var ='
< div class =select-location-page>
< h2>我们应该在哪里见到你?< / h2>
< p class =location-sub-header>填写此表单,我们将在1小时内与您会面!< / p>
< form action =http://www.example.com/mail.phpmethod =POSTclass =location-form>
< div class =device-details>
< h2>装置:iPhone5< / h2>
< h2>修复费用:$< span id =result> 0< / span>< / h2>
< / div>
< input name =nametype =textid =nameplaceholder =Namemaxlength =20>
< input name =numbertype =textid =numberplaceholder =Numbermaxlength =15>
< input name =addresstype =textid =addressplaceholder =Addressmaxlength =40>
< input name =citytype =textid =cityplaceholder =Citymaxlength =20>
< input name =ziptype =textid =stateplaceholder =Zipmaxlength =20>
< input name =devicetype =hiddenid =devicevalue =iPhone5maxlength =20>
< div class =submit-btn>
< input type =submitvalue =从电子邮件提交请求/>
< / div>
< / form>
< script>
document.getElementById(result)。innerHTML = localStorage.getItem(sum);
< / script>
< / div>
';
if(!empty($ _ POST ['email_from'])){
ob_start();
echo $ var;
$ output = ob_get_contents();
ob_end_clean();
$ to = $ _POST ['email_from']; //发送到用户输入的电子邮件
$ from =email@example.com; //这应该是你的电子邮件地址
$ subject =表单提交;
$ message = $ output;
$ headers ='MIME-Version:1.0'。 \r\\\
;
$ headers。='Content-type:text / html; charset = iso-8859-1'。 \r\\\
;
$ headers。=From:。从$;
mail($ to,$ subject,$ message,$ headers);
}
再次,javascript可能无法正常工作这个,所以你可能需要考虑另一种方式,或者只是省略它。
hello i am wondering if there is any way to send a block of html code to an email address. i have a variable that is set in an external javaScript file named "sum" and i am calling on that variable in my html code, therefore one part of the html block is being dynamically populated. I cannot figure out how to email this the normal php way so im just wondering if i could just send the entire block of html code to an email address.
here is the entire block of html code i would like to send:
<div class="select-location-page"> <h2>Where should we meet you?</h2> <p class="location-sub-header">Fill out this form and we will meet you within 1 hour!</p> <form action="../mail.php" method="POST" class="location-form"> <div class="device-details"> <h2>Device: iPhone5</h2> <h2>Repair Cost: $<span id="result">0</span></h2> </div> <input name="name" type="text" id="name" placeholder="Name" maxlength="20"> <input name="number" type="text" id="number" placeholder="Number" maxlength="15"> <input name="address" type="text" id="address" placeholder="Address" maxlength="40"> <input name="city" type="text" id="city" placeholder="City" maxlength="20"> <input name="zip" type="text" id="state" placeholder="Zip" maxlength="20"> <input name="device" type="hidden" id="device" value="iPhone5" maxlength="20"> <div class="submit-btn"> <input type="submit" value="Submit Request" /> </div> </form> <script> document.getElementById("result").innerHTML = localStorage.getItem("sum"); </script> </div>
here is the php file i tried to create, all of it works except the repair cost (ive tried declaring a php variable in the html code and then putting it in the mail.php file but it did not work)
<?php $name = $_POST['name']; $number = $_POST['number']; $address = $_POST['address']; $city = $_POST['city']; $zip = $_POST['zip']; $device = $_POST['device']; $formcontent=" From: $name \n number: $number \n Address: $address \n City: $city \n Zip $zip \n Device: $device \n Repair Cost: (need this to work)"; $recipient = "joe.t.oconnor@gmail.com"; $subject = "Device Repair Request"; $mailheader = "From: $name \r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); header('Location: thankyou.html'); ?>
this is kind of a confusing question, any help would be greatly appreciated. thanks!
解决方案(Consult my Edit below)
You can use output buffering to capture the form and use a full http call for the action and send as HTML.
Sidenote: Be sure to change all instances
example.com
to your server address.Example:
<?php ob_start(); echo ' <form action="http://www.example.com/mail.php" method="POST" class="location-form"> <div class="device-details"> <h2>Device: iPhone5</h2> <h2>Repair Cost: $<span id="result">0</span></h2> </div> <input name="name" type="text" id="name" placeholder="Name" maxlength="20"> <input name="number" type="text" id="number" placeholder="Number" maxlength="15"> <input name="address" type="text" id="address" placeholder="Address" maxlength="40"> <input name="city" type="text" id="city" placeholder="City" maxlength="20"> <input name="zip" type="text" id="state" placeholder="Zip" maxlength="20"> <input name="device" type="hidden" id="device" value="iPhone5" maxlength="20"> <div class="submit-btn"> <input type="submit" value="Submit Request" /> </div> </form> '; $output = ob_get_contents(); ob_end_clean(); $to = "email@example.com"; $from = "email@example.com"; $subject = "Form submission"; $message = $output; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From:" . $from; mail($to,$subject,$message,$headers);
Then in your
mail.php
file:Sidenote: I left out some of the POST arrays. You'll need to add those yourself. This is merely an example of something that worked for me.
<?php $first_name = $_POST['name']; // The following is non-existant in your form. You can adjust to your liking. $message = $_POST['message']; $to = "email@example.com"; $from = "email@example.com"; $subject = "Form submission 2"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From:" . $from; mail($to,$subject,$message,$headers);
Footnotes:
You may not be able to use JS here, but you can try it. I didn't use it in my test.
You'll also have to use some form of button or link to tell people to click to send. This is an experimental answer. I will end up modifying it as soon as I can.
References:
- http://php.net/manual/en/function.ob-start.php
- http://php.net/manual/en/function.ob-get-contents.php
- http://php.net/manual/en/function.ob-end-clean.php
Edit:
Here's how this (new one, edited) works. (You will need to add your other POST arrays to
mail.php
as discussed in comments).Sidenote: See comments in code for some added instructions.
// comment...
- The user enters their email address (taken from a separate form).
- If it's not empty, capture the email in a POST array.
- If the email is not empty, capture the whole thing in the output buffer.
- Sends the form to the user.
New code:
<?php echo $form = ' <form action="" method="POST" class="location-form"> <input name="email_from" type="text" id="email_from"> <input type="submit" name="submit" value="Submit Request" /> </form> '; if(!empty($_POST['email_from']) ){ // This outputs a message to the user after entering their email, and submits. echo $from2 = "An email has been sent to: " . $_POST['email_from']; } $var = ' <div class="select-location-page"> <h2>Where should we meet you?</h2> <p class="location-sub-header">Fill out this form and we will meet you within 1 hour!</p> <form action="http://www.example.com/mail.php" method="POST" class="location-form"> <div class="device-details"> <h2>Device: iPhone5</h2> <h2>Repair Cost: $<span id="result">0</span></h2> </div> <input name="name" type="text" id="name" placeholder="Name" maxlength="20"> <input name="number" type="text" id="number" placeholder="Number" maxlength="15"> <input name="address" type="text" id="address" placeholder="Address" maxlength="40"> <input name="city" type="text" id="city" placeholder="City" maxlength="20"> <input name="zip" type="text" id="state" placeholder="Zip" maxlength="20"> <input name="device" type="hidden" id="device" value="iPhone5" maxlength="20"> <div class="submit-btn"> <input type="submit" value="Submit Request from Email" /> </div> </form> <script> document.getElementById("result").innerHTML = localStorage.getItem("sum"); </script> </div> '; if(!empty($_POST['email_from']) ){ ob_start(); echo $var; $output = ob_get_contents(); ob_end_clean(); $to = $_POST['email_from']; // Send to the email entered by the user $from = "email@example.com"; // This should be YOUR E-mail address $subject = "Form submission"; $message = $output; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From:" . $from; mail($to,$subject,$message,$headers); }
Again, the javascript may not work for this, so you might have to think of another way or just omit it.
这篇关于如何发送邮件动态块的html代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!