2

I am trying to make a page in html where I can have a form and when you press submit it sends an email to someone with the output of the form without opening an email client like gmail, outlook.

Here is my code and what I tried

<form action="mailto:info@w3docs.com" method="post" enctype="text/plain">
    <div>
      <label for="name">First-Name:
        <input type="text" name="name" id="name" />
      </label>
    </div>

    <div>
        <label for="sname">Surname:
          <input type="text" name="surname" id="name" />
        </label>
      </div>

    <div>
      <label for="email">Email:
        <input type="text" name="email" id="email" />
      </label>
    </div>

    <div>
        <label for="phone-number">Phone Number:
          <input type="text" name="phone-number" id="email" />
        </label>
      </div>

      <label for="Gender">Gender</label>
      <select id="Gender" name="Gender">
        <option value="" disabled selected>Select Your Gender</option>
        <option value="Male">Male</option>
        <option value="Female">Female</option>
        <option value="Other">Other</option>
        <option value="Rather Not Say">Rather Not Say</option>
      </select>

      <div>
        <label for="Password">Password:
          <input type="text" name="PS" id="name" />
        </label>
      </div>

    <div>
      <input type="submit" name="submit" value="Send" />
    </div>
  </form>
Joseph
  • 21
  • 2
  • [eh](https://stackoverflow.com/questions/37642845/sending-an-email-from-the-browser-using-js). you will really need a backend, a server that will send that message for you. – Yarin_007 Nov 20 '22 at 11:36

1 Answers1

0

Yeah you can send an email without opening an client like gmail & outlook but you need and smtp server to do that. You can get gmail's smtp credentials which is username: your-email-id@gmail.com and password: create an app passwsord. Then you can send email through php or python using php-mailer with following code

You can download php-mailer through this link https://storage.googleapis.com/aryan-rh-cdn.appspot.com/Public-Uploads/php-mailer_221120_114205.tar

//inlcude php-mailer library
require_once("php-mailer/PHPMailer.php");
require_once("php-mailer/SMTP.php");
require_once("php-mailer/Exception.php");
use PHPMailer\PHPMailer\PHPMailer;
function sendMail($name, $email, $body, $subject, $date)
{

  $mail = new PHPMailer();
  $mail->isSMTP();
  $mail->Host = "smtp.gmail.com";
  $mail->SMTPAuth = true;
  $mail->Username = "your gmail id";
  $mail->Password = "app specific password";
  $mail->Port = 587;

  $mail->setFrom("your email id":, $name);
  $mail->addReplyTo("reply-to-your-gmail@gmail.com", $name); // reply to address usually not needed
  $mail->addAddress($email);

  $mail->isHTML(false);
  $mail->Subject = $subject;
  $mail->Body = $body;

  if ($mail->send()) {
    $mail_send_status = true;
  } else {
    $mail_send_status = false;
  }
    if ($mail_send_status == true) {
       echo "mail is successfully sent to " . $email;
    }else{
      echo "An error occured while sending email";
    }
}
Aryan R.H
  • 7
  • 1
  • 2