php 单击按钮自动发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5650237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending emails automatically at the click of a button
提问by Daniel H
I am designing an Emergency Response page, and one of the features we need is to be able to click a button (e.g. 'Send details to embassy'), and then send an automatically-generated email to the intended recipient ($email_address
) without having to go into Microsoft Outlook and click send. Is there a way to do this?
我正在设计一个紧急响应页面,我们需要的功能之一是能够单击一个按钮(例如“向大使馆发送详细信息”),然后将自动生成的电子邮件发送给预期的收件人 ( $email_address
),而无需进入 Microsoft Outlook 并单击发送。有没有办法做到这一点?
The only method I know is the <a href='mailto:[email protected]'>
one, but this opens the email in Outlook and really I need it to be completely automated.
我知道的唯一方法是<a href='mailto:[email protected]'>
一种,但这会在 Outlook 中打开电子邮件,我真的需要它完全自动化。
回答by gmadd
Something like this would work as a starting point:
像这样的事情可以作为起点:
<form action="" method="post">
<input type="submit" value="Send details to embassy" />
<input type="hidden" name="button_pressed" value="1" />
</form>
<?php
if(isset($_POST['button_pressed']))
{
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
UPDATE
更新
This can be used as a Javascript function to call the mail.php page and send the email without reloading the page.
这可以用作 Javascript 函数来调用 mail.php 页面并在不重新加载页面的情况下发送电子邮件。
function sendemail()
{
var url = '/mail.php';
new Ajax.Request(url,{
onComplete:function(transport)
{
var feedback = transport.responseText.evalJSON();
if(feedback.result==0)
alert('There was a problem sending the email, please try again.');
}
});
}
You'll need Prototype for this method: http://www.prototypejs.org/api/ajax/request
您将需要此方法的原型:http: //www.prototypejs.org/api/ajax/request
I haven't tested this, but hopefully it should be along the right lines.
我还没有测试过这个,但希望它应该是正确的。
回答by Rhapsody
PHP supports sending email with the mail function. You can find examples at the PHP documentation. (see link)
PHP 支持使用邮件功能发送电子邮件。您可以在 PHP 文档中找到示例。(见链接)
Example from PHP documentation:
PHP 文档中的示例:
<?php
// The message
$message = "Line 1\nLine 2\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
// Send
mail('[email protected]', 'My Subject', $message);
?>