How to send 2 mails in html form
To send two emails in an HTML form, you'll need to use a server-side programming language like PHP, Python, or JavaScript (using a library like Node.js). Here's a general outline of the steps:
Method 1: Using PHP
- Create an HTML form with two input fields for the email addresses:
<form action="send_emails.php" method="post"> <input type="email" name="email1" placeholder="Email 1"> <input type="email" name="email2" placeholder="Email 2"> <input type="submit" value="Send Emails"> </form>
- Create a PHP script (
send_emails.php
) that will handle the form submission:<?php // Get the email addresses from the form $email1 = $_POST['email1']; $email2 = $_POST['email2'];
// Set the email subject and body $subject = 'Test Email'; $body = 'This is a test email sent from an HTML form.';
// Send the first email $headers = 'From: Your Name [email protected]'; mail($email1, $subject, $body, $headers);
// Send the second email $headers = 'From: Your Name [email protected]'; mail($email2, $subject, $body, $headers);
// Redirect the user to a success page header('Location: success.php'); exit; ?>
**Method 2: Using JavaScript (with Node.js)**
1. Create an HTML form with two input fields for the email addresses:
```html
<form id="email-form">
<input type="email" name="email1" placeholder="Email 1">
<input type="email" name="email2" placeholder="Email 2">
<button type="submit">Send Emails</button>
</form>
- Create a JavaScript file (
script.js
) that will handle the form submission:const form = document.getElementById('email-form'); form.addEventListener('submit', (e) => { e.preventDefault(); const email1 = form.email1.value; const email2 = form.email2.value;
// Set the email subject and body const subject = 'Test Email'; const body = 'This is a test email sent from an HTML form.';
// Send the first email using Node.js const nodemailer = require('nodemailer'); const transporter = nodemailer.createTransport({ host: 'your_smtp_host', port: 587, secure: false, // or 'STARTTLS' auth: { user: 'your_email_username', pass: 'your_email_password', }, });
const mailOptions = { from: 'Your Name [email protected]', to: email1, subject, text: body, };
transporter.sendMail(mailOptions, (error, info) => { if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } });
// Send the second email const mailOptions2 = { from: 'Your Name [email protected]', to: email2, subject, text: body, };
transporter.sendMail(mailOptions2, (error, info) => { if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } });
// Redirect the user to a success page window.location.href = 'success.html'; });
**Method 3: Using Python (with Flask)**
1. Create an HTML form with two input fields for the email addresses:
```html
<form action="/send_emails" method="post">
<input type="email" name="email1" placeholder="Email 1">
<input type="email" name="email2" placeholder="Email 2">
<input type="submit" value="Send Emails">
</form>
- Create a Python script (
app.py
) that will handle the form submission using Flask:from flask import Flask, request, redirect, url_for import smtplib
app = Flask(name)
@app.route('/send_emails', methods=['POST']) def send_emails(): email1 = request.form['email1'] email2 = request.form['email2']
subject = 'Test Email'
body = 'This is a test email sent from an HTML form.'
# Set up the email server
server = smtplib.SMTP('your_smtp_host', 587)
server.starttls()
server.login('your_email_username', 'your_email_password')
# Send the first email
msg = f'Subject: {subject}\n\n{body}'
server.sendmail('[email protected]', email1, msg)
# Send the second email
msg = f'Subject: {subject}\n\n{body}'
server.sendmail('[email protected]', email2, msg)
# Close the email server
server.quit()
# Redirect the user to a success page
return redirect(url_for('success'))
if name == 'main': app.run(debug=True)
In all cases, make sure to replace the placeholders (`your_smtp_host`, `your_email_username`, `your_email_password`, etc.) with your actual email server settings.