I am designing an Emergency Response page, and one of the feat开发者_开发问答ures 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?
The only method I know is the <a href='mailto:example@test.com'>
one, but this opens the email in Outlook and really I need it to be completely automated.
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 = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\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.
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
I haven't tested this, but hopefully it should be along the right lines.
PHP supports sending email with the mail function. You can find examples at the PHP documentation. (see link)
Example from PHP documentation:
<?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('caffeinated@example.com', 'My Subject', $message);
?>
精彩评论