-1

I have a question, how do I create a button that sends someone an email WITHOUT opening an email program and that works. (It is a button in a website!)

I have been informed about the mailto:, but my computer keeps opening up a email program rather than actually sending an email in my gmail. Please help!

  • 1
    you'll need server-side script that talks with smtp. something like [php mailer](https://github.com/PHPMailer/PHPMailer) – Bagus Tesa Nov 23 '15 at 15:05
  • why not use html form to send to php mail() function – domii Nov 23 '15 at 15:06
  • 1
    Possible duplicate of [How do I send email with JavaScript without opening the mail client?](http://stackoverflow.com/questions/14268796/how-do-i-send-email-with-javascript-without-opening-the-mail-client) – Kenster Nov 23 '15 at 16:29

2 Answers2

1

I think you will need to have a backend system to do this for you and then make your HTML button call it.

On the backend you will need to have a mail service running, such as Amazon SES. So the scope of your problem looks bigger than just writing some javascript code.

Lucas Pottersky
  • 1,844
  • 6
  • 22
  • 38
0

Well, PHP can do this easily.

It can be done with the PHP mail() function. Here's what a simple function would look like:

<?php     
$to_email = 'name@company.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
$headers = 'From: noreply@company.com';
mail($to_email,$subject,$message,$headers);
?>

This will send a background e-mail to the recipient specified in the $to_email.

The above example uses hard coded values in the source code for the email address and other details for simplicity.

Let’s assume you have to create a contact us form for users fill in the details and then submit.

  1. Users can accidently or intentional inject code in the headers which can result in sending spam mail
  2. To protect your system from such attacks, you can create a custom function that sanitizes and validates the values before the mail is sent.

Let’s create a custom function that validates and sanitizes the email address using the filter_var() built in function.

Here's an example code:

<?php 
function sanitize_my_email($field) {
    $field = filter_var($field, FILTER_SANITIZE_EMAIL);
    if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
        return true;
    } else {
        return false;
    }
}
$to_email = 'name@company.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail ';
$headers = 'From: noreply@company.com';
//check if the email address is invalid $secure_check
$secure_check = sanitize_my_email($to_email);
if ($secure_check == false) {
    echo "Invalid input";
} else { //send email 
    mail($to_email, $subject, $message, $headers);
    echo "This email is sent using PHP Mail";
}
?>

We will now let this be a separate PHP file, for example sendmail.php.

Then, will use this file on form submission, using the action attribute of the form, like:

<form action="sendmail.php" method="post">
   <input type="text" value="Your Name: ">
   <input type="password" value="Set Up A Passworrd">
   <input type="submit" value="Signup">
   <input type="reset" value="Reset Form">
</form>

Hope I could help

SK-the-Learner
  • 523
  • 5
  • 18