I'm adding Google reCaptcha to this PHPMailer form.
It should be sent via SMTP.
How should I verificate reCaptcha and send current form? How if statement should be written here?
This my index.html file's code:
<form class="form-subscribe" action="mail.php" method="post">
<div class="container">
<div class="row">
<input type="text" class="form-control col-md-5 form-name" name="name" placeholder="Ваше имя" required>
<div class="col-md-1"></div>
<input type="text" class="form-control col-md-6 form-email" name="email" placeholder="Ваша эл. почта" required>
<textarea type="text" class="form-control form-text bg-gray col-md-12" name="message" placeholder="Ваше сообщение" required></textarea>
</div>
<div class="input-group-append">
<div class="g-recaptcha" data-sitekey=""></div>
<button class="button button-shadow2" type="submit" name="submit">Отправить</button>
</div>
</div>
</form>
This is mail.php file's code:
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($email);
$mail->addAddress('');
$mail->isHTML(true);
$mail->Subject = 'Сообщение с сайта';
$mail->Body = '' .$name . ' оставил заявку, его телефон ' .$email. '<br>Сообщение этого посетителя: ' .$message;
$mail->AltBody = '';
if(!$mail->send()) {
echo 'Message sent!';
} else {
echo 'Error!';
}
?>
It´s not so complicated as it looks,
i´ve given you an example in the comments of your question.
Now i´ll give you a little walk through.
Notice: This example was not tested but i´ve made a syntax check.
Your html code is okay so there no changes needed.
As you already know you´ve only to add your key:
<div class="g-recaptcha" data-sitekey="YOUR_KEY"></div>
Now we will get the "recaptcha data":
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
Recptcha needs some data to validate the captcha:
$data = array(
'secret' => 'YOUR_SECRET',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
Next the script must get all data and verfiy the captcha,
with file_get_contents it will load the recaptcha data and
decode it with json:
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
At least we need a if loop to check if the captcha is valid or not:
if ($captcha_success->success==false) {
echo "false";
} else if ($captcha_success->success==true) {
echo "true";
}
The next step is quick & dirty:
if ($captcha_success->success==false) {
echo "Captcha wrong";
} else if ($captcha_success->success==true) {
if(!$mail->send()) {
echo 'Message sent!';
} else {
echo 'Error!';
}
}
So the complete code would look like this:
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($email);
$mail->addAddress('');
$mail->isHTML(true);
$mail->Subject = 'Сообщение с сайта';
$mail->Body = '' .$name . ' оставил заявку, его телефон ' .$email. '<br>Сообщение этого посетителя: ' .$message;
$mail->AltBody = '';
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => 'YOUR_SECRET',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "Captcha wrong";
} else if ($captcha_success->success==true) {
if(!$mail->send()) {
echo 'Message sent!';
} else {
echo 'Error!';
}
}
Here is to origial tutorial (json):
https://www.kaplankomputing.com/blog/tutorials/recaptcha-php-demo-tutorial/
This page provides a tutorial with ajax too:
https://www.kaplankomputing.com/blog/tutorials/php/setting-recaptcha-2-0-ajax-demotutorial/
Related
So the emails I recieve only shows the subject, who it was sent from, who it was sent to but not the senders name or phone number, this is not done locally but from where my website is hosted. I want it to show the senders name and phone number after the body. This is my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $_POST['message'];
$mail->Name = $_POST['name'];
$mail->Phone = $_POST['number'];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
//echo $mail->ErrorInfo;
?>
This is my html:
<form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">
<div class="input-grids">
<input type="text" name="name" placeholder="Full name or Business"
class="contact-input" required=""/>
<input type="email" name="email" placeholder="Your email" class="contact-
input" required=""/>
<input type="text" name="subject" placeholder="Subject" class="contact-
input" required=""/>
<input type="text" name="number" placeholder="Phone number"
class="contact-input" />
</div>
<div class="form-input">
<textarea name="message" placeholder="Type your message here"
required=""></textarea>
</div>
<div class="form-input mb-5">
<label class="container"><p>Send me a copy <a href="#">privacy policy.
</a></p>
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
<button class="btn submit">Submit</button>
</form>
I am not sure if I need to do something like this:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
}
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
and just give my submit button this attribute
<button class="btn submit" name="submit">Submit</button>
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You basically want to create a $message variable and assign that to $mail->Body.
The second example you have shown is close but you need to make sure that the below is within your $_POST['submit'] if statement.
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
Like so:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
$response = $mail->send(); //to actually send the email
}
For:
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You could subject the form via ajax and listen to the $response value. If its 1 then good to go, else there was an issue.
I am having some problems with php. Well, the code, when uploaded to the server and run in browser is working and the email is arriving, using this code below:
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
$name = "catarina";
$email = "myEmail#hotmail.com";
$message = "test";
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The problem is, when I change this code a little to send with a form, the email never arrives:
js:
function submitForm() {
$.ajax({type:'POST', url:'email-action.php', data:$('#contact-form').serialize(), success: function(result){
$('.submit').html('send');
$('.send').removeClass('no-show');
$('.send').removeClass('no-show-mobile');
}});
};
html:
<form action="/" onsubmit="return submitForm();" method="post" name="contactform" class="contact-form wow zoomIn" data-wow-delay="0.6s" id="contact-form">
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<input placeholder="Name" class="input-field" name="name" required="" type="text">
</div>
<div class="col-md-12">
<input placeholder="E-mail" class="input-field" name="email" required="" type="email">
</div>
</div>
</div>
<div class="col-md-6">
<textarea placeholder="Message" class="input-field" name="message"></textarea>
<input value="Send Message" class="input-send submit" type="submit" name="submit">
</div>
<div class="col-md-12 send no-show hidden-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully! We will get in touch shortly.</h4></button></div>
<div class="col-md-12 send no-show-mobile visible-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully!</h4></button></div>
</form>
php:
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
///this is the thing that change for one script to another
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
/////
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I made a contactform which send (if filled in) a mail to the email that's in the code. I want a contactform that send a confirmation email to the person that fills in the form AND to the owner of the site. So the customer knows he filled in the form in the site.
If this makes any sense? I dont know how to explain it better.
CODE: Index.php
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact form</title>
<link rel="stylesheet" href="css/style.css"/>
<script src="js/script.js"></script>
</head>
<body>
<div class="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul>
<li>
<?php echo implode('</li><li>', $errors); ?>
</li>
</ul>
</div>
<?php endif; ?>
<form action="contact.php" method="post">
<label>
Your name*
<input type="text" name="name" id="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
</label>
<label>
Your email address *
<input type="email" name="email" id="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
</label>
<label>
Your message *
<textarea name="message" id="contact" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
</label>
<input type="submit" value="Send">
<p class="muted">* Means a required field</p>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
CODE: contact.php
<?php
session_start();
require_once "libs/phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
"message" => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Subject';
$mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .') <p>'. $fields['message'] .'</p>';
$mail->FromName = $fields['name'];
$mail->AddAddress('rainier.laan#home.nl', 'Rainier Laan');
if($mail->send()){
header('Location: bedankt.php');
die();
} else {
echo $mail->ErrorInfo; exit;
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
I hope you guys can help me out with this and can provide me with code i can use. If something i said are unclear please say so. This code is without this function that i explained above, This is only with the function that the owner gets a mail not the customer. Rainier laan.
Use below code for contact.php
<?php
session_start();
require_once "libs/phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
"message" => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Someone filled in your contactform';
$mail->Body = $fields['name'].' filled in your form with the following message: ' .$fields['message'];
$mail->FromName = $fields['name'];
$mail->AddAddress('rainier.laan#home.nl', 'Rainier Laan'); //added mail id of owner
if($mail->send()){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML();
$mail->SMTPDebug = 2;
$mail->Subject = 'Confirmation contactform';
$mail->Body = 'Thank you for filling in our form.<br> Message: <p>'. $fields['message'] .'</p>';
$mail->FromName = 'Owner';
$mail->AddAddress($fields['email] , $fields['name]); //added mail id of user
if($mail->send()){
header('Location: bedankt.php');
die();
}
else{
exit;
}
} else {
echo $mail->ErrorInfo; exit;
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
I made a PHP mailer but it's not working anymore. It used to work but i changed nothing. It doesnt send the email. I filled in all the neccesary information for it to work but it doesnt. I know im not quite clear with my problem that because i dont know what the problem is and im not really good if it comes to PHP.
Here's the code.
This is the INDEX.PHP
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact form</title>
<link rel="stylesheet" href="css/style.css"/>
<script src="js/test.js"></script>
</head>
<body>
<div class="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul>
<li>
<?php echo implode('</li><li>', $errors); ?>
</li>
</ul>
</div>
<?php endif; ?>
<form action="contact.php" method="post">
<label>
Your name*
<input type="text" name="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
</label>
<label>
Your email address *
<input type="email" name="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
</label>
<label>
Your message *
<textarea name="message" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
</label>
<input type="submit" value="Send">
<p class="muted">* Means a required field</p>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
This is the CONTACT.PHP
<?php
session_start();
require_once "libs/phpmailer/PHPMailerAutoload.php";
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
"message" => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
// 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
// 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
// 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp1.example.com';
$mail->Username = 'example';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 69;
$mail->isHTML();
$mail->Subject = 'Contact form submitted';
$mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .') <p>'. $fields['message'] .'</p>';
$mail->FromName = 'Contact';
$mail->AddAddress('email', 'name');
if($mail->send()){
header('Location: bedankt.php');
die();
} else {
$errors[] = 'Sorry we konden de email niet verzenden, Probeer later nog een keer.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('location: index.php');
I hope you guys can help me with the information i've given here.
If something is missing please say so
UPDATE
The problem has been solved. I had nothing to do with the code but it was an isseu with the host, Thanks for the help!
First: In contact.php are you sure about port 69 and why not 587 ?
if you are using ziggo the $mail->Host must be smtp.ziggo.nl
Uitgaande serverinstellingen
server: smtp.ziggo.nl
serverpoort: 587
Use ssl: type versleuteling: STARTTLS, TLS of SSL
Be carefull with $mail->isSMTP(); that it is true $mail->isSMTP(true);
working example:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
require $path.'/phpmailer/PHPMailerAutoload.php'; //the downloaded zip with PHPMailerAutoload i have put into a directoru called phpmailer!!
//require 'PHPmailer.php';
$_error = '';
// insert configs
$_smtpAuth = 'true'; //true
$_smtpSecure = 'SSL'; // TLS og SSL
$_smtpHost = 'send.one.com'; //one.com has send.one.com //gmail has smtp.gmail.com
$_smtpPort = '465';
$_smtpUsername = 'xxx#qwerty.com'; //your email address or username
$_smtpPassword = 'xxxXxxxy'; //your password
$mail = new PHPmailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(true); // Set mailer to use SMTP
$mail->Host = $_smtpHost;//$_smtpHost.';smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = $_smtpAuth; // Enable SMTP authentication
$mail->Username = $_smtpUsername; // SMTP username
$mail->Password = $_smtpPassword; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $_smtpPort; // TCP port to connect to
$mail->setFrom('name#yourwebmail.com', 'Mailer');
$mail->addAddress('', ''); // Add a recipient
$mail->addAddress(''); // Name is optional
$mail->addReplyTo('', ''); //$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('name#awebmail.com'); //Recipient The sender of the email
$mail->addBCC('');
$mail->addAttachment(''); // Add attachments
$mail->addAttachment(''); // 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';
}
?>
date_default_timezone_set('Europe/Berlin');
require("class.phpmailer.php");
$mail = new phpmailer();
$mail->IsSMTP(); // per SMTP verschicken
$mail->Host = "example.com"; // SMTP-Server
$mail->SMTPAuth = true; // SMTP mit Authentifizierung benutzen
$mail->Username = "example"; // SMTP-Benutzername
$mail->Password = "example"; // SMTP-Passwort
$mail->From = "test#example.com";//any address
$mail->FromName = "example";
$mail->AddAddress($to,"");
$mail->IsHTML(true); // als HTML-E-Mail senden
$mail->Subject = "Confirmation mail ";
$mail->Body = "<html><body>Dear example <br/><br/><br/> \nKind regards\n<br/><br/> KM </body></html>";
if(!$mail->Send())
{
echo "error";
}
else
{
echo "send";
}
Here is index.html codes;
<form name="contactform" method="POST" action="send.php">
Name: <input type="text" name="ad_soyad" size="25"><br />
Telephone: <input type="text" name="tel" size="25"><br />
E-Mail: <input type="text" name="email" size="25"><br />
Address: <textarea rows="5" name="adres" cols="25"></textarea><br />
Message: <textarea rows="5" name="mesaj" cols="25"></textarea><br />
<input type="submit" name="button" value="Gonder">
</form>
Here is send.php codes:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'blablabla.com';
$mail->SMTPAuth = true;
$mail->Username = 'blablabla.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
if(($ad_soyad=="") or ($tel =="") or ($email=="") or ($mesaj=="")){
echo "<center>Please fill the required fields.<br><a href=index.html>Go back</a></center>";
}
else
{
$mail->setFrom($_POST['email']);
$mail->addAddress('contact#blablabla.com', 'BlaBlaBla');
$mail->isHTML(true);
$mail->Subject='Contact Form Message';
$mail->Body = ($_POST['mesaj']);
$smtp = new SMTP;
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;
if(!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent.';
}
}
?>
It works.
But only sends name,email and message.
So, I want to add more field like telephone or address or etc.
but i can not put them into $mail->body when i tried to write telephone and message together
it gives me error:"not configured properly "
I have to use SMTP because my hosting only provides SMTP..
how can i send all datas which form gets from user ,with SMTP?
I don't know if this will solve the issue - I can't understand why it should fail if you add more content to the message body but as you have not shown that I can only guess it was done in such a way as to make invalid markup?
<?php
/* Are these variables defined before this point? */
if( ( $ad_soyad=="" ) or ( $tel =="" ) or ( $email=="" ) or ( $mesaj=="" ) ){
echo "<center>Please fill the required fields.<br><a href=index.html>Go back</a></center>";
} else {
/* only load the libraries if the above variables are not empty */
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'blablabla.com';
$mail->SMTPAuth = true;
$mail->Username = 'blablabla.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
/* some basic filtering of user supplied variables */
$email=filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
$message=filter_input( INPUT_POST, 'mesaj', FILTER_SANITIZE_STRING );
$name=filter_input( INPUT_POST, 'ad_soyad', FILTER_SANITIZE_STRING );
$tel=filter_input( INPUT_POST, 'tel', FILTER_SANITIZE_STRING );
$address=filter_input( INPUT_POST, 'adres', FILTER_SANITIZE_STRING );
/* prepare message components */
$msg=array();
$msg[]="Name:".$name;
$msg[]="Phone:".$tel;
$msg[]="Address:".$address;
$msg[]="Message:".$message;
$mail->setFrom( $email );
$mail->addAddress('contact#blablabla.com', 'BlaBlaBla');
$mail->isHTML(true);
$mail->Subject='Contact Form Message';
$mail->Body = implode( "\r\n", $msg );
$smtp = new SMTP;
//Enable connection-level debug output
$smtp->do_debug = SMTP::DEBUG_CONNECTION;
if( !$mail->send() ) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent.';
}
$mail = $smtp = null;
}
?>