3

I'd like to be able to send an email by entering a URL like

example.com/api/send-email?email=bob@example.org&name=Robert&template=job2231 

then an email is sent based on a pre-configured template that takes in the email, name parameters.

I would have logged into example.com via their web UI, so ideally authentication is handled in the background - wrt to sending the email. Of course if not logged in, the email isn't sent.

Do you have any recommendations for example.com or an equivalent product? Can be hosted or self-hosted.

  • This should be a comment, but I don't have enough reputation. Would this be a regular link and the email would be sent as soon as the user opens it in the browser? In this case users might send it too often by accident. Sometimes when they visit it by themselves, sometimes when browsers sync automatically. There was a story of a developer who added garage door automation via a link and this happened to him: twitter.com/rombulow/status/990684453734203392 . One workaround would be asking the user to press a button to confirm sending the email. Is this acceptable? – Andrew Runner Jun 13 '18 at 12:07
4
+50

Self hosting solution (cheap and configurable, but you take up the risks and responsibilities of securing it):

Create a single index.php file and host it on a PHP server

<?php 
if(isset($_POST['submit'])){
    $to = $_POST['email'];
    $from = "mailer@yourcompany.com";
    $name = $_POST['name'];
    $subject = "Sample Subject";
    $message = $name . " " . " wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

    // If you want to have a success page, uncomment the next line
    // header('Location: thank_you.php'); 
    // to redirect to another page.
    // you need to create another file named thank_you.php
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

EDIT: For authentication, you can do something simple using HTTP auth (It's not super secure or anything, but it just does the job)

<?php
    $AUTH_USER = 'admin';
    $AUTH_PASS = 'admin';
    header('Cache-Control: no-cache, must-revalidate, max-age=0');
    $has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
    $is_not_authenticated = (
        !$has_supplied_credentials ||
        $_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
        $_SERVER['PHP_AUTH_PW']   != $AUTH_PASS
    );
    if ($is_not_authenticated) {
        header('HTTP/1.1 401 Authorization Required');
        header('WWW-Authenticate: Basic realm="Access denied"');
        exit;
    } else {
        // all that code from above goes here...
    }
?>  
  • Note: The asker also wants authentication, it is easy with a server like Apache but you might want to add a paragraph about that :-) – Nicolas Raoul Jun 18 '18 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.