How to pass contact us details to mail through code
To pass contact us details to a mail through code, you can use a programming language like PHP, Python, or JavaScript, depending on your website's technology stack. Here are some examples:
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = '[email protected]';
$subject = 'Contact Us Form Submission';
$body = "Name: $name\n";
$body.= "Email: $email\n";
$body.= "Message: $message\n";
$headers = 'From: '. $email. "\r\n".
'Reply-To: '. $email. "\r\n".
'MIME-Version: 1.0'. "\r\n".
'Content-Type: text/plain; charset=UTF-8';
mail($to, $subject, $body, $headers);
?>
In this example, the contact form data is passed through the $_POST
superglobal array. The script then constructs a mail message using the form data and sends it using the mail()
function.
Python (using Flask):
from flask import Flask, request
import smtplib
app = Flask(__name__)
@app.route('/contact', methods=['POST'])
def contact():
name = request.form['name']
email = request.form['email']
message = request.form['message']
to_email = '[email protected]'
subject = 'Contact Us Form Submission'
body = f"Name: {name}\nEmail: {email}\nMessage: {message}"
msg = f"Subject: {subject}\n\n{body}"
server = smtplib.SMTP('your_smtp_server')
server.sendmail(to_email, to_email, msg)
server.quit()
return 'Thank you for contacting us!'
In this example, the contact form data is passed through the request.form
dictionary. The script then constructs a mail message using the form data and sends it using the smtplib
library.
JavaScript (using Node.js and Nodemailer):
const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
app.post('/contact', (req, res) => {
const name = req.body.name;
const email = req.body.email;
const message = req.body.message;
const transporter = nodemailer.createTransport({
host: 'your_smtp_server',
port: 587,
secure: false, // or 'STARTTLS'
auth: {
user: '[email protected]',
pass: 'your_password'
}
});
const mailOptions = {
from: email,
to: '[email protected]',
subject: 'Contact Us Form Submission',
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
res.send('Thank you for contacting us!');
});
});
In this example, the contact form data is passed through the req.body
object. The script then constructs a mail message using the form data and sends it using the nodemailer
library.
Remember to replace the placeholders ([email protected]
, your_smtp_server
, etc.) with your actual email address, SMTP server, and other credentials.