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");
Related
I want to send mails using this PHP MAILING script but through SMTP and not through sendmail because my host rejects all emails as spam and I don't check my spam folder as its heavy loaded with spam emails daily.
I just want to know where to put the code which will use my SMTP logins to send mails using this php script instead of using sendmail/
I tried adding some things like
$host = "smtp.gmail.com";
$port = "587";
$username = "testtest#gmail.com";
$password = "testtest";
But these didn't worked, actually it broke the whole code itself,
Edit: I also tried adding this, reading an answer from another answered question here, and it also broke the code itself but didn't work.
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'johndoe#gmail.com',
'password' => 'passwordxxx'
Original Code
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
//only shows if prevent default fails (js in index.html)
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
//only shows if prevent default fails (js in index.html)
$errorMSG .= "E-mail is required ";
} else {
$email = $_POST["email"];
}
// MESSAGE
if (empty($_POST["message"])) {
//only shows if prevent default fails (js in index.html)
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
// change this to fit !
$EmailTo = "demo#domain.com";
$Subject = "New Message from MENTOR";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Somethin went wrong...";
} else {
echo "An error has ocurred!";
//echo $errorMSG; Enable if you want full details of php error
}
}
?>
I know the title is weird I cant for the life of me phrase it well lol.
I have done searches with multiple ways of phrasing the question and nothing shows up for this.
I have the email scripting working on the website im building and its fantastic! but when i edited the mail code to add extra message lines its made the sequence go wrong.
here is the code im using for the email message area:
<?php
require_once "Mail.php";
// load the variables form address bar
$name = $_REQUEST["name"];
$subject = 'Customer Feedback';
$message = $_REQUEST["message"];
$from = $_REQUEST["from"];
$compname = $_REQUEST["companyName"];
$ph = $_REQUEST["phone"];
$acp = $_REQUEST['allowCommentPublish'];
$marketing = $_REQUEST['incmarketing'];
$verif_box = $_REQUEST["verif_box"];
// Checking the check boxes and marking as appropriate
if(isset($_POST['allowCommentPublish']))
{
$acp = 'Yes';
}
else
{
$acp = 'No';
}
if(isset($_POST['incmarketing']))
{
$marketing = 'Yes';
}
else
{
$marketing = 'No';
}
// Optional data checker
if($compname == '')
{
$compname = 'N/A';
}
if($ph == '')
{
$ph = 'N/A';
}
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$message = stripslashes($message);
$subject = stripslashes($subject);
$acp = stripcslashes($acp);
$marketing = stripcslashes($marketing);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon'])
{
// if verification code was correct send the message and show this page
$ToEmail = "email#email.com";
$message = "Name: ".$name."\n".$message;
$message = "From: ".$from."\n".$message;
$message = "Comments: ".$message."\n".$message;
$message = "Allow feedback to be Published: ".$acp."\n".$message;
$message = "[ OPTIONAL DATA ]"."\n".$message;
$message = "Company Name: ".$compname."\n".$message;
$message = "Phone Number: ".$ph."\n".$message;
$message = "Allow extra Marketing? ".$marketing."\n".$message;
$headers = array ('From' => $from,
'To' => $ToEmail,
'Subject' => 'Feedback: '.$subject);
$smtp = Mail::factory('smtp', array ('host' => 'smtp.vic.exemail.com.au', 'auth' => false));
$mail = $smtp->send($ToEmail, $headers, $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
header("Location: /feedback_sent.php");
exit;
}
else
{
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
In my mind this should spit out the message body as this:
Name: name here
From: Email address
Comments: Message here
Allow feedback to be published: response
[ OPTIONAL DATA ]
Company Name: Company
Phone Number: Phone
Allow extra Marketing:
This should be how its seen in the email right?
What im actually getting is this:
Allow feedback to be Published: response
[ OPTIONAL DATA ]
Company Name: company
Phone Number: phone
Allow extra Marketing? Response
From: Email address
Name: name here
Comments: Message here
Is this normal? or have i inadvertently snuffed it somewhere along the lines and its messing with my head as payment?
Thanks for any help on this.
EDIT: Updated code.
<?php
// -----------------------------------------
// The Web Help .com
// -----------------------------------------
// remember to replace your#email.com with your own email address lower in this code.
require_once "Mail.php";
// load the variables form address bar
$name = $_REQUEST["name"];
$subject = 'Customer Feedback';
$comment = $_REQUEST["message"];
$from = $_REQUEST["from"];
$compname = $_REQUEST["companyName"];
$ph = $_REQUEST["phone"];
$acp = $_REQUEST['allowCommentPublish'];
$marketing = $_REQUEST['incmarketing'];
$verif_box = $_REQUEST["verif_box"];
// Checking the check boxes and marking as appropriate
if(isset($_POST['allowCommentPublish']))
{
$acp = 'Yes';
}
else
{
$acp = 'No';
}
if(isset($_POST['incmarketing']))
{
$marketing = 'Yes';
}
else
{
$marketing = 'No';
}
// Optional data checker
if($compname == '')
{
$compname = 'N/A';
}
if($ph == '')
{
$ph = 'N/A';
}
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$comment = stripslashes($comment);
$subject = stripslashes($subject);
$acp = stripcslashes($acp);
$marketing = stripcslashes($marketing);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon'])
{
// if verification code was correct send the message and show this page
$ToEmail = "jim#digital2go.com.au";
$message = "Name: ".$name."\n".$message;
$message .= "From: ".$from."\n".$message;
$message .= "Comments: ".$comment."\n".$message;
$message .= "Allow feedback to be Published: ".$acp."\n".$message;
$message .= "[ OPTIONAL DATA ]"."\n".$message;
$message .= "Company Name: ".$compname."\n".$message;
$message .= "Phone Number: ".$ph."\n".$message;
$message .= "Allow extra Marketing? ".$marketing."\n".$message;
$headers = array ('From' => $from,
'To' => $ToEmail,
'Subject' => 'Feedback: '.$subject);
$smtp = Mail::factory('smtp', array ('host' => 'smtp.vic.exemail.com.au', 'auth' => false));
$mail = $smtp->send($ToEmail, $headers, $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
header("Location: /feedback_sent.php");
exit;
}
else
{
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
Make your message "continue" in the order you wish by doing this:
$message = "Name: ".$name."\n".$message;
$message .= "From: ".$from."\n".$message;
$message .= "Comments: ".$message."\n".$message;
$message .= "Allow feedback to be Published: ".$acp."\n".$message;
$message .= "[ OPTIONAL DATA ]"."\n".$message;
$message .= "Company Name: ".$compname."\n".$message;
$message .= "Phone Number: ".$ph."\n".$message;
$message .= "Allow extra Marketing? ".$marketing."\n".$message;
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"];
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.';
}
}
?>