I have no idea how much work I've lost because of this... but my form doesn't work correctly.
While I get the emails, they are completely blank. My code follows:
<span class="errmsg"></span>
<label for="contact-name">Name</label>
<input type="text" id="contact-name" name="contact-name" required>
<label for="contact-email">E-mail address</label>
<input type="email" id="contact-email" name="contact-email" required>
<label for="contact-subject">Subject</label>
<input type="text" id="contact-subject" name="contact-subject" required>
<label for="contact-message">Message</label>
<textarea id="contact-message" name="contact-message" required></textarea>
<div class="button" type="submit" id="contact-submit" name="contact-submit">Submit</div>
Rather than go for a classic <form>...</form> construct, I've opted to use an ajax post to send the data to PHP:
$("#contact-submit").click(function() {
// validateForm() just checks that required fields have values
if (validateForm()) {
$.ajax({
type: 'POST',
url: '/contact.php',
data: { name: $('#contact-name').val(),
from: $('#contact-email').val(),
subject: $('#contact-subject').val(),
message: $('#contact-message').val()
}, // end data
success: function clearFields() {
$('#contact-name').val('');
$('#contact-email').val('');
$('#contact-subject').val('');
$('#contact-message').val('');
$('.errmsg').text('Your email was sent successfully.');
$('.errmsg').css('color', '#389320');
} // end success
}); // end ajax
}
else
{
var errmsg = "Your email could not be sent.<br />";
errmsg += "Please ensure that you've filled in all the fields.";
$(".errmsg").html(errmsg);
$(".errmsg").css("color", "#ff0000");
}
}); // end click
This (should) post the form data to the PHP file below which sends off the email:
<?php
error_reporting(E_ALL);
require_once("class.phpmailer.php");
include("class.smtp.php");
$email = new PHPMailer();
try
{
//
// Set server details for send
//
$email->IsSMTP();
$email->Host = "mail.this.com";
$email->Port = 25;
$email->SMTPAuth = true;
$email->Username = "info#this.com";
$email->Password = "p455W0rd";
//
// Send mail from the contact form
//
$to = "info#this.com";
$from = $_POST["contact-email"];
$name = $_POST["contact-name"];
$subject = "From web: ".$_POST["contact-subject"];
$message = $_POST["contact-message"];
$body = "<p>Hi Ortund,</p>";
$body .= "<p>You have received a new query on your website.</p><p>Please see below:</p>";
$body .= "<p>";
$body .= str_replace("\r\n", "<br />", $message);
$body .= "</p>";
$body .= "</body></html>";
$email->SetFrom($from, $name);
$email->AddReplyTo($from, $name);
$email->AddAddress($to, $to);
$email->Subject = $subject;
$email->Body = $body;
$email->IsHTML(true);
$email->Send();
session_start();
$_SESSION["mailresult"] = "success";
http_redirect("http://www.this.com");
}
catch (Exception $e)
{
$_SESSION["mailresult"] = "failed";
$_SESSION["mailerror"] = $e->getMessage();
}
?>
There's a couple of layers of checks to return a message back to the user as to what happened, but none work and this is also a major problem for me...
Right now I'd be happy to get the email form data into the email, though I can't understand why it isn't there...
If anyone can help me out here, I'd really appreciate it.
EDIT
Here's an example of an email I'm getting now:
From web:
Root user:
Sent: Mon 2014/05/26 12:24 PM
To: info#this.com
Hi Ortund,
You have received a new query on your website.
Please see below:
data are posted using this names
data: { name: $('#contact-name').val(),
from: $('#contact-email').val(),
subject: $('#contact-subject').val(),
message: $('#contact-message').val()
},
but retrieved with different name here
$from = $_POST["contact-email"];
$name = $_POST["contact-name"];
$subject = "From web: ".$_POST["contact-subject"];
$message = $_POST["contact-message"];
so use like this
$from = $_POST["from"];
$name = $_POST["name"];
$subject = "From web: ".$_POST["subject"];
$message = $_POST["message"];
Related
I bought a bootstrap theme just to make a simple site. It has a contact form on it and the theme creator said to make sure your hosting let's you use php. I asked my host and they said:
"You need to add email authentication to the form.
Create an email address in cpanel for use with the form, then use that
user name and password to authenticate the email sending from the
form.
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm"
I created the e-mail in cpanel, but unfortunately, I am not knowledgable enough for the next step. I was hoping someone could help me with the code to get this form working.
This is the email.php code in it's entirety:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = "Message";
$body = "From $name, $email, \n\n$message";
$to = "myemail#gmail.com";
mail($to, $subject, $body);
?>
Is there something simple I can add to this to authenticate and make the form work?
Updated answer:
You could edit your email.php / html code to the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send email example</title>
</head>
<body>
<form method="post">
<input type="text" name="sender_email" placeholder="Senders email address">
<input type="text" name="receiver_email" placeholder="Receivers email address">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit">Send email</button>
</form>
</body>
</html>
<?php
require_once('send_email_class.php');
if(!empty($_POST)){
$sender_email = $_POST['sender_email'];
$receiver_email = $_POST['receiver_email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_email = new send_email_class($sender_email, $receiver_email, $subject, $message);
$send_email_response = $send_email->send_email();
echo $send_email_response;
}
?>
and upload the following code as a file called: send_email_class.php to your webserver.
<?php
class send_email_class {
// Class constructor storing variables
function __construct($sender, $receiver, $subject, $message){
$this->receiver = $receiver;
$this->subject = $subject;
$this->message = $message;
$this->header = 'From: ' . $sender . '\r\n';
}
// Function to send the email
function send_email(){
if(mail($this->receiver, $this->subject, $this->message, $this->headers)){
return 'Mail sent.';
}else{
return 'Unable to send mail.';
}
}
}
?>
Ofcourse you don't have to enter all those informations from the form, you could store some informations like the $sender_email variable fixed in your PHP code.
But thats up to you. Hope this helped.
Edit:
Please replace the content of the email.php with the following:
<?php
class email{
// Class constructor storing variables
function __construct($sender_email, $sender_name, $message){
$this->sender = $sender_email;
$this->sender_name = $sender_name;
$this->message = $message;
$this->receiver = 'changeme#email.com';
$this->subject = 'Email from: ' . $sender_name;
$this->header = 'From: ' . $sender_email . '\r\n';
}
// Function to send the email
function send_email(){
if(mail($this->receiver, $this->subject, $this->message, $this->header)){
return 'Thank you ' . $this->sender_name . ' for sending the email containing the following message: ' . $this->message;
}else{
return 'Sorry ' . $this->sender_name . ' a error occured, please try again.';
}
}
}
if(!empty($_POST)){
$sender_email = $_POST['email'];
$sender_name = $_POST['name'];
$message = $_POST['message'];
$send_email = new email($sender_email, $sender_name, $message);
$send_email_response = $send_email->send_email();
echo $send_email_response;
}
?>
And edit the following code line: $this->receiver = 'changeme#email.com';
Final answer:
After receiving the files, the fix was easy, the ajax been dealing with the handling itself.
mail.php content:
<?php
$receiver = 'xxxxx#gmail.com';
$subject = 'Email from: ' . $_POST['name'];
$message = $_POST['message'];
$header = 'From: ' . $_POST['email'];
mail($receiver, $subject, $message, $header);
?>
While I am sending the email on the pop up of submit button I used if(isset($_POST['submit'])) but it does not takes the value.
On submit button so tell me what is the problem in this code it will not accept submit as isset post.
In the if function the of isset the submit button not inputting the values also explain if there is jquery issue
<?php
//print_r($_POST);die();
ini_set('display_errors', 'On');
if(isset($_POST['submit'])){
$to = "basavraj.p#wepearl.in"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = trim($_POST['fname']);
/*$last_name = $_POST['last_name'];*/
$email = trim($_POST['email']);
$phone_number = trim($_POST['phone-number']);
$subject = trim($_POST['subject']);
$description = trim($_POST['description']);
$quote_sub = trim($_POST['quote-sub']);
$message = "First Name: ".$first_name."\r\n Email: ".$email." \r\n Phone Number: ".$phone_number." \r\n Subject: ".$subject." \r\n Description: ".$description." " ;
$headers = "From: kashmira.s#wepearl.in" . "\r\n" .
"CC: pooja.s#wepearl.in";
/*$headers2 = "From:" . $to;*/
if(mail($to,$quote_sub,$message,$headers))
{
echo "success";
}
else
{
echo "failed";
}
}
?>
I think your query
if(isset($_POST['submit'])){
fails because you might not have added name="submit" in <input type="submit" /> statement or you may have someother name.
So, add
<input type="submit" name="submit" value="Go!" />
in your html page. In php, just check whether form has been submitted or not by,
if(isset($_POST['submit'])){
To check, just echo the submitted data by,
if(isset($_POST['submit']))
{
$from = $_POST['email'];
echo "From email : " . $from;
/ ..add rest here like name.../
Email form not sending and can't figure out why. I used the advice form this post tp create the form. Submit form and a textarea on ajax post
the reason for doing this is to greate a IE8 and 9 friendly form that works.
The on click function....
$("form#example-advanced-form").click(function() {
//alert("submitting?");
// validate() just checks that required fields have values
//if (validate()) {
$.ajax({
type: 'POST',
url: 'PHPMailer/sendMyform.php',
data: { name: $('#name').val(),
from: $('#email').val(),
subject: $('#role').val(),
message: $('#coverletter').val()
},// end data
success: function clearFields() {
$('#name').val('');
$('#email').val('');
} // end success
}); // end ajax
/*}
else
{
var errmsg = "Your email could not be sent.<br />";
errmsg += "Please ensure that you've filled in all the fields.";
$(".errmsg").html(errmsg);
$(".errmsg").css("color", "#ff0000");
}*/
$('form#example-advanced-form').submit();
}); // end click
The send mail form receiving the data...
error_reporting(E_ALL);
require_once("class.phpmailer.php");
include("class.smtp.php");
$email = new PHPMailer();
try
{
//
// Set server details for send
//
$email->IsSMTP();
$email->Host = "****";
$email->Port = 25;
$email->SMTPAuth = true;
$email->Username = "****";
$email->Password = "****";
//
// Send mail from the contact form
//
$to = "****";
$from = $_POST["email"];
$name = $_POST["name"];
$subject = "From web: ".$_POST["role"];
$message = $_POST["name"];
$body = "<p>Hi,</p>";
$body .= "<p>You have received a new query on your website.</p><p>Please see below:</p>";
$body .= "<p>";
$body .= str_replace("\r\n", "<br />", $message);
$body .= "</p>";
$body .= "</body></html>";
$email->SetFrom($from, $name);
$email->AddReplyTo($from, $name);
$email->AddAddress($to, $to);
$email->Subject = $subject;
$email->Body = $body;
$email->IsHTML(true);
$email->Send();
session_start();
$_SESSION["mailresult"] = "success";
http_redirect("http://www.this.com");
}
catch (Exception $e)
{
$_SESSION["mailresult"] = "failed";
$_SESSION["mailerror"] = $e->getMessage();
}
I get a error Call to undefined function on line 49 which is...
http_redirect("http://www.this.com");
I got the messege to apear in the DIV tag but not sure how to send the email address onto the external email database //myguestlist.com/mgl/formreceiver.php.
here is the HTML
Be the first to know about the Channel Seven Brisbane Racing
Carnival:
method="post" id="mf528c0a39d76be"> <input class="mailchimp-input"
input="" name="PatronEmail" id="mf528c0a39d76be_PatronEmail"
placeholder="Enter your email..." type="text"> <button type="submit"
class="btn"><span class="singup-image"><img src="img/send.png"
alt="send email"></span><span class="singup-text">Send</span></button>
</form>
<div class="success-message"></div>
<div class="error-message"></div>
</div>
Here is the PHP:
<?php
// Email address verification
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]]).)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]).){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email));
}
if($_POST) {
// Enter the email where you want to receive the notification when someone subscribes
$emailTo = 'wbriton#brc.com.au';
$subscriber_email = ($_POST['PatronEmail']);
if(!isEmail($subscriber_Email)) {
$array = array();
$array['valid'] = 0;
$array['message'] = 'Insert a valid email address!';
echo json_encode($array);
}
else {
$array = array();
$array['valid'] = 1;
$array['message'] = 'Thanks for your subscription!';
echo json_encode($array);
// Send email
$subject = 'New Subscriber!';
$body = "You have a new subscriber!\n\nEmail: " . $subscriber_email;
// uncomment this to set the From and Reply-To emails, then pass the $headers variable to the "mail" function below
//$headers = "From: ".$subscriber_email." <" . $subscriber_email . ">" . "\r\n" . "Reply-To: " . $subscriber_email;
mail($emailTo, $subject, $body);
}
}
?>
And here is the Java Script
$('.success-message').hide();
$('.error-message').hide();
$('.subscribe form').submit(function() {
var postdata = $('.subscribe form').serialize();
$.ajax({
type: 'POST',
url: 'php/sendmail.php',
data: postdata,
dataType: 'json',
success: function(json) {
if(json.valid == 0) {
$('.success-message').hide();
$('.error-message').hide();
$('.error-message').html(json.message);
$('.error-message').fadeIn();
}
else {
$('.error-message').hide();
$('.success-message').hide();
$('.subscribe form').hide();
$('.success-message').html(json.message);
$('.success-message').fadeIn();
}
}
});
return false;
});
We have a PHP form on our website that has worked for years. When users fill the form out in sends us the details, and sends them an email and then re-directs the user to a "thank-you" page. However, if a user puts their email address as #gmail.com the form fails to send, it doesnt show the thank-you page.
This is really bizarre. We are using PHPMailer and validate if the form has been sent using:
if($mail->Send()) {
$mailsent = true;
If a user enters #hotmail.com or at #yahoo.com everything is fine. But enter #gmail.com and $mailsent is always false.
In this situation where does the problem lye? It seems to me that our web hosts SMTP connection to Gmail is failing. Would this seem right?
Here is the code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$name = '';
$email = '';
$mailsent = false;
$referer = '';
if(isset($_POST['name'])){
include('phpmailer/class.phpmailer.php');
$name = $_POST['name'];
$email = $_POST['email'];
$subject = 'Quote request from ' . $name;
$body = 'A quote request has been received from a user with following details:' . "\n\n" .
"Name: $name \n" .
"Email: $email \n" .
"----------------------------------------------------\n\n" .
"Place: UK".
$body .= "\n----------------------------------------------------\n\n";
$body .= "Refering: ".$referer." \n";
$mail = new PHPMailer();
$mail->Subject = $subject;
$mail->From = $email;
$mail->FromName = $name;
$mail->Body = $body;
$mail->AddAddress("dean#example.com", "Example");
if($mail->Send()) {
$mailsent = true;
} else {
$error[] = 'There was some error. Please try later.';
}
}
?>