At the bottom of my site there is a "send message" button. I want it to take the message and contact information(email, name) and send it to my email address. How could I possibly do this? I'm new at this website stuff by the way.
2 Answers
You can use PHP mail function.
<?php
if (!empty($_POST)) {
$name= $_POST['name'];
$mail_id= $_POST['email_id'];
$email= '';//email address on which you want to receive website details
$message_field = $_POST['message'];
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Return-Path: \r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header= "From: TriaaHousing";
$message="Name:".$name."\r\n";
$message .=" ";
$message .= "EmailId:".$mail_id."\r\n";
$message .=" ";
$message .= "Message:".$message_field;
if(mail($email, "Subject", $message, $header)){
echo 1;
}else{
echo 0;
}
}
?>

- 90
- 1
- 7
Use can use default PHP's mail() functionality OR you can use PHPMailer (Mail send helper). Both are safe and correct. but if you need some other stuff then use PHPMailer.
1. Using PHP's mail() function it's possible. Remember mail function will not work in Local server.
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
NOTE: You need to config SMTP on your local server if you are using SMTP. Take a look at this similar post.
2. You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer .
It allows you to use the mail function or use an smtp server transparently. It also handles HTML based emails and attachments so you don't have to write your own implementation.
Here is an example from the page above:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('webmaster@example.com', 'Webmaster User'); // Add a recipient
$mail->addAddress('webmaster@example.com'); // Name is optional example
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Use addReplyTo
, addCC
, addBCC
if require.
Hope this help you well!

- 1
- 1

- 21,025
- 5
- 26
- 57