Sending emails automatically at the click of a button
Asked Answered
A

2

6

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?

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.

Applecart answered 13/4, 2011 at 13:40 Comment(1)
since you mentioned PHP, use PHP to do it, what you describe is a mailto link that is recognized by Outlook on a webpage not PHP code that shoots off an email/text/notice.Conceal
T
12

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.

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.

Towage answered 13/4, 2011 at 13:50 Comment(1)
thank you! it's about 3 days I working for do this!!Bioscopy
W
1

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('[email protected]', 'My Subject', $message);
?> 
Weisbart answered 13/4, 2011 at 13:42 Comment(4)
Thanks Rhapsody, this works well. The problem I have now is that when I use this method inside a mail.php file, and I submit the form then the page redirects. I need the page that I am sending the email from to be completely untouched because it has important stopwatches on it and these will be reset if redirected.Applecart
It's probably worth looking at making an Ajax call to the mail.php script, this would stop the page loading, but run the script.Towage
Hi gmadd, would you be able to post an example of this please? I haven't had a chance to look into AJAX in much detail yet so I don't know how to do use it. ThanksApplecart
@user683526 I've updated my answer with some Ajax code, untested.Towage

© 2022 - 2024 — McMap. All rights reserved.