Personally I use Swiftmailer as my email client that way I don’t have to come up with my own script(s). I am in the process of starting my own small printing company and in the process of designing and developing the website for it. I just so happen to be in in the middle of doing my contact page and I have the form done. I show it it to you for maybe it will give you pointers or help?
The whole thing is basically on one page with the exception of some configuration pages and Swiftmailer. Here’s the PHP and HTML
[php]<?php
include_once ‘vendor/swiftmailer/swiftmailer/lib/swift_required.php’;
require_once ‘lib/includes/utilities.inc.php’;
/*
- Send Email to Adminstrator
*/
function send_email(array $data) {
/* Setup swiftmailer using your email server information */
if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
$transport = Swift_SmtpTransport::newInstance(EMAIL_HOST, EMAIL_PORT); // 25 for remote server 587 for localhost:
} else {
$transport = Swift_SmtpTransport::newInstance(EMAIL_HOST, 25);
}
$transport->setUsername(EMAIL_USERNAME);
$transport->setPassword(EMAIL_PASSWORD);
/* Setup To, From, Subject and Message */
$message = Swift_Message::newInstance();
$name = $data['name'];
$subject = $data['reason'];
$email_from = $data['email'];
if (trim($data['phone']) !== NULL) {
$comments = $name . ' (' . $data['phone'] . ') wrote the following: ' . $data['comments'];
} else {
$comments = $name . ' wrote the following: ' . $data['comments'];
}
$message->setTo([
'[email protected]' => 'John Smith'
]);
$message->setSubject($subject); // Subject:
$message->setBody($comments); // Message:
$message->setFrom($email_from, $name); // From and Name:
$mailer = Swift_Mailer::newInstance($transport); // Setting up mailer using transport info that was provided:
$result = $mailer->send($message, $failedRecipients);
if ($result) {
return TRUE;
} else {
echo "<pre>" . print_r($failedRecipients, 1) . "</pre>";
return FALSE;
}
}
$error = [];
function blankCheck($temp) {
$error = isset($temp) ? trim($temp) : ‘’;
if ($error == “”) {
return true;
}
}
$submit = filter_input(INPUT_POST, ‘action’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (isset($submit) && $submit === ‘enter’) {
/* The Following to get response back from Google recaptcah */
$url = “https://www.google.com/recaptcha/api/siteverify”;
$remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
$response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer);
$recaptcha_data = json_decode($response);
/* The actual check of the recaptcha */
if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) {
$data['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['phone'] = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['reason'] = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['comments'] = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$error[0] = blankCheck($data['name']);
$error[1] = blankCheck($data['email']);
$error[2] = blankCheck($data['comments']);
if (!in_array(true, $error, true)) {
send_email($data);
}
}
}
require_once ‘lib/includes/header.inc.php’;
?>
<fieldset>
<legend>Contact Form</legend>
<input type="hidden" name="action" value="enter">
<label for="name" accesskey="U">Your Name</label>
<input name="name" type="text" id="name" placeholder="Enter your name" required="required" />
<label for="email" accesskey="E">Email</label>
<input name="email" type="email" id="email" placeholder="Enter your Email Address" required="required" />
<label for="phone" accesskey="P">Phone <small>(optional)</small></label>
<input name="phone" type="tel" id="phone" size="30" placeholder="Enter your phone number" />
</fieldset>
<fieldset>
<legend>Your Comments</legend>
<div class="radioBlock">
<input type="radio" id="radio1" name="reason" value="question" checked>
<label class="radioStyle" for="radio1">question</label>
<input type="radio" id="radio2" name="reason" value="order status">
<label class="radioStyle" for="radio2">status</label>
<input type="radio" id="radio3" name="reason" value="comment">
<label class="radioStyle" for="radio3">comment</label>
</div>
<label class="textBox" for="comments">Comments</label>
<textarea name="comments" id="comments" placeholder="Enter your comments (required)" spellcheck="true" required="required"></textarea>
<div class="g-recaptcha" data-sitekey="6Lel2hsUAAAAAOGxdZaaZ2BwcMBC7sSr86P4f6QG"></div>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</div>
<?php
require_once 'lib/includes/footer.inc.php';
[/php]
I still have to write a “This email was successfully sent!” or “The were a problem sending, please correct and try again” message, but it works. I also rely on HTML5 to successfully block in sending a blank email, but I have written some php to backup in case it doesn’t work. I have found out in the past that some mobile device and tablets don’t recognize HTML5 for some strange reason? I have also added Google’s reCaptcha in hopes of stopping most spammers.
Anyway to see it in action click here -> https://www.buystuffright.com/contact.php
Anyways I hope this is some help?
John
P.S. I just notice I should really log the error message if the email fails instead of showing the error for my production website. I’ll be correcting that.