PHP Examples
Simple PHP Send Mail Class for sending emails from a web page
PHP is a server-side language, and to use it on webpages, you need to integrate it with HTML and JavaScript. Below is an example of an HTML send mail form.
/*HTML*/
<div class="sendmailbox">
<form method="post" name="sendmailform" id="sendmailform">
<div>
<label for="email">email:</label>
<input required type="email" name="email" id="email" size="20" minlength="6" maxlength="100" placeholder="type your email here" />
</div>
<div>
<label for="message">message:</label>
<textarea required name="message" id="message" cols="20" rows="10" placeholder="type your message here">
</textarea>
</div>
<div>
<input type="submit" name="SendMail" id="SendMail" value=" Send " />
</div>
</form>
</div>
Certainly, if you want to add Google reCAPTCHA to your HTML form for better security and to prevent automated submissions from bots, here's how you can modify your form:
HTML Form with Google reCAPTCHA:
/*HTML*/
< !DOCTYPE html >
<html>
<head>
<!-- Add the Google reCAPTCHA site key in place of 'YOUR_RECAPTCHA_SITE_KEY' -->
<script src="https://www.google.com/recaptcha/api.js" async defer>
</script>
</head>
<body>
<div class="sendmailbox">
<form method="post" name="sendmailform" id="sendmailform" onsubmit="return validateForm()">
<div>
<label for="email">Email:</label>
<input required type="email" name="email" id="email" size="20" minlength="6" maxlength="100" placeholder="Type your email here" />
</div>
<div>
<label for="message">Message:</label>
<textarea required name="message" id="message" cols="20" rows="10" placeholder="Type your message here">
</textarea>
</div>
<!-- Add the reCAPTCHA widget here -->
<div class="g-recaptcha" data-sitekey="YOUR_RECAPTCHA_SITE_KEY">
</div>
<div>
<input type="submit" name="SendMail" id="SendMail" value="Send" />
</div>
</form>
</div>
<!-- JavaScript to validate the reCAPTCHA -->
<script> function validateForm() { var response = grecaptcha.getResponse(); if (response.length === 0) { alert("Please complete the reCAPTCHA."); return false; } } </script>
</body>
</html>
In the form above:
Added the reCAPTCHA widget from Google. You need to replace 'YOUR_RECAPTCHA_SITE_KEY' with your actual reCAPTCHA site key.
Included a JavaScript function validateForm() that checks whether the user has completed the reCAPTCHA. If the reCAPTCHA is not completed, it displays an alert message and prevents the form submission.
This setup ensures that users must complete the reCAPTCHA challenge before they can submit the form, helping to prevent automated submissions from bots.
PHP Code:
This is a straightforward PHP mail-sending class designed for web pages. It's highly customizable to suit your specific requirements. It doesn't limit you to sending emails; you can use it to submit various types of messages with just a bit of customization.
/*PHP*/
<?php
if (isset($_POST['SendMail'])) {
$E = new SendMail();
$E->To = 'recipient@example.com';
//Replace with your recipient's email
$E->Subject = isset($_POST['Subject']) ? $_POST['Subject'] : $E->Subject;
$E->Type = 'text/plain; charset=utf-8';
//Adjust content type if needed eg text/html
$result = $E->Send() ? 'Email Sent!' : 'Failed!';
echo $result;
}
class SendMail {
public $rqrdFlds;
public $ntrdFlds;
public $To;
public $Subject;
public $Type;
public function __construct() {
$this->rqrdFlds = array('email', 'message');
$this->ntrdFlds = array('SendMail');
$this->Subject = 'Mail from ' . $_SERVER['HTTP_HOST'];
}
public function Send() {
$body = '';
foreach ($_POST as $field => $value) {
$value = trim(rawurldecode($value));
//use rawurldecode if you use javascript ajax to post it.
if (in_array($field, $this->rqrdFlds) && empty($value)) {
return false;
}
$value = stripslashes(htmlspecialchars($value));
if ($field == 'email' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
return false;
}
if ($field == 'email') {
$this->From = $value;
}
if (!in_array($field, $this->ntrdFlds)) {
$body .= $field . ":\r\n" . $value . "\r\n\r\n";
}
}
$head = "From: " . $this->From . "\r\n";
$head .= "Content-Type: " . $this->Type . "\r\n";
return mail($this->To, $this->Subject, $body, $head);
}
}
?>
Be sure to replace 'recipient@example.com' in the PHP code with the actual email address where you want to receive the emails. enjoy!