Need a PHP email send script for HTML an form - php

I have a single page portfolio website that I'm building and I have a contact form that I need to process using php send script. I'm a novice when it comes to PHP so I'm having trouble getting this to work. I've done some searching but I can't find what I'm looking for.
Here's what I have done, I copied this from a PHP contact page that I had built but the PHP and form are on the same page and I need an external send.php to process my form.
<?php
$error = ''; // error message
$name = ''; // sender's name
$email = ''; // sender's email address
$company = ''; // company name
$subject = ''; // subject
$comment = ''; // the message itself
if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
// the email will be sent here
// make sure to change this to be your e-mail
$to = "example#email.com";
// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want
$subject = '[Contact Form] : ' . $subject;
// the mail message ( add any additional information if you want )
$msg = "From : $name \r\ne-Mail : $email \r\nCompany : $company \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
}
}
if(!isset($_POST['send']) || $error != '')
{
header("location: http://www.#.com/#contact");
}
?>
So for my form I want to have:
<form method="post" action="send.php" class="form">
I plan on using HTML5 and jQuery to validate the form, so I really only need the script to capture the info and send the email to a single address. After it sends I want the script to redirect back to the Contact page.
Edit:
I found a solution after spending a while on google.
http://www.website.com/#contact");
?>

For one thing, you haven't initialized the value for $message
$message = stripslashes($message);
You probably meant to use $comment instead of $message

Not sure what problem you're facing. Simply copy the PHP you have into a file called send.php and my first glance says it'll work if you change $message back to $comment and add error checking. If you still have issues, post back with more details.

Related

PHP Mail sending email on page refresh

Ahoy!
Any help on this would be really appreciated! I have a contact form which is sending emails on a page refresh. I have tried a number of things and am not getting anywhere.. Heres what Ive got so far:
if(isset($_POST['email']) && $_POST['email'] != '') {
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messageSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "email#gmail.com";
$body = "";
$body .= "From: " .$userName. "\r\n";
$body .= "Email: " .$userEmail. "\r\n";
$body .= "Message: " .$message. "\r\n";
header('Location: http://www.website.net/contact-thank-you.html');
exit();
mail($to, $messageSubject, $body);
}
}
?>
The easiest way to prevent this is to redirect to a new URL after the email has been sent. Often websites will redirect to a "thank you" page.
You might be able to get away with redirecting to the same URL (and this won't send a second email because $_POST will be empty).
Another way would be to use the user session ($_SESSION) to keep track of whether the email was sent, and don't send an email again if you detect that $_SESSION indicates it was already sent. You could also use cookies for this same purpose. Last but not least you could store that information in a database and check the DB before sending an email so you don't send multiple emails.

Mail/mime email not sent when displaying form with jquery

I have a relatively basic email form put together which I intend to email to a recipient using mail mime. This approach has worked for me in the past when the form was displayed on its own page i.e. a contact page.
Here I am using jquery to display a hidden div which contains my form from the home page. The problem is that although my validations pass the email is not being sent. I am at a loss for why and hope someone here can help me out!
The relevant php is as follows:
$path = get_include_path() . PATH_SEPARATOR . '/home/host/php';
set_include_path($path);
include('Mail.php');
include('Mail/mime.php');
$recipient_email = 'somebody#somewhere.com';
//validations are here
//send the email
$name = $_POST['name'];
$visitor_email = $_POST['from'];
$subject = $_POST['subject'];
$user_message = $_POST['message'];
$to = $recipient_email;
$from = $visitor_email;
$text = "From: $name \n Subject: $subject \n Message $user_message";
$message = new Mail_mime();
$message->setTXTBody($text);
$body = $message->get();
$extraheaders = array("From"=>$name, "To"=>$recipient_email, "Subject"=>$subject, "Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
echo "Success";
The jquery to display the form looks like this:
jQuery.fn.fadeToggle = function(speed, easing, callback) {
return this.animate({opacity: 'toggle'}, speed, easing, callback);
};
$(document).ready(function() {
$('#tellfriend').hide();
$('li a.email, #tellfriend a.close').click(function() {
$("#tellfriend").fadeToggle('slow');
});
});
One of my thoughts (and I'll experiment with it) is that because when the link is clicked to display form, the link is appended to the URL and so it may affect my form action? Not sure.. Any help is appreciated!
I was able to solve my problem by following a previous question I eventually found on Stack Overflow HERE
It turns out that I had to specify an outgoing mail server in order to send the form out. Still, it is odd that I have had it working in the past where I didn't need to follow such steps.

How to get cookie value from user's browser and email it to webmaster after form submit

I have a form on my site which users are filling with name and email, and then it generates an email to the webmaster notifying of user submitting their info. There's also a cookie placed which is for referral tracking, how can I also include that data in the email out to the webmaster?
<?php
///subscribe form
$email = $_POST['email'] ;
$firstname = $_POST['firstname'] ;
$recipient = "myemail#email.com"; /// Webmaster address
if (isset($_POST['email']))
{
//Send Mail To Webmaster
$email = $_POST['email'] ;
$subject = ' Enviralizer: Someone Just Requested More Info';
$message = 'Your new lead info is below: ';
$message .= "\r\n\nName: " .$firstname;
$message .= "\r\n\nEmail: " .$email;
mail("$recipient", $subject,
$message, "From:" . $recipient);
header("Location: http://thanks.com");
}
?>
I know the script is not the best secured thing in the world, I will take care of that later, I just need a quick fix for a bigger script tracking issue I'm trying to resolve as well. Thanks.
I guess you're looking for $_COOKIE['name_of_cookie'] ?

jquery Contact Form and getting an email with message-text

I made a contact form based on this tutorial but I can't get this one to work.
When I hit the submit button, nothing happens (it seems like the page is being refreshed or on my live website it returns to the index.html) and I don't get an email and no response.
I want to get an email containing the content of the form that the user filled out.
This is the ajaxSubmit.php:
<?php
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "xxx#gmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
if (!empty($name) & !empty($email) && !empty($body)) {
$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
$send = mail($receiver, 'Contact Form Submission', $body, "From: {$email}");
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
}
?>
xxx#gmail.com is just a placeholder here for my regular email adress, of course.
FIDDLE with html, js and css.
What am I doing wrong?
I reccomend using isset, to check whether the posted variables actually exists, furthermore: I recommend using the ternary operator to check if they are actually set:
$name = isset($_POST['name']) ? $_POST['name'] : false;
$email = isset($_POST['email']) ? $_POST['email'] : false;
$web = isset($_POST['web']) ? $_POST['web'] : false;
$text = isset($_POST['text']) ? $_POST['text'] : false;
$receiver = "xxx#gmail.com"; // You forgot to close a whitespace here.
if(($name && $email && $web && $text) != false)
{
// It's okay to send email now.
// Checkpoint.
}
I recommend using this spam check filtering function:
function spamcheck($field)
{
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return true;
}
else
{
return false;
}
}
And I like to use this modified mail function:
// Usage: sendMail($toEmail, $fromEmail, $subject, $message);
// Pre: $toEmail is of type string and is a valid email address,
// indicating the receiver.
// $fromEmail is of type string and is a valid email address,
// indicating the sender.
// $subject is of type string, indicating the email subject.
// $message is of type string, indicating the message to be send
// via the email, from $fromEmail's address to $toEmail's address.
// Post: An email containing $message and the subject $subject from
// $fromEmail's address to $toEmail's address if $fromEmail
// does not indicate a spam email.
function sendMail($toEmail, $fromEmail, $subject, $message)
{
$validFromEmail = spamcheck($fromEmail);
if($validFromEmail)
{
mail($toEmail, $subject, $message, "From: $fromEmail");
}
}
So you can continue from where we loft off. To begin with: You don't really need the curly braces. Also.. you're overwriting the $body variable, it would make more sense to keep the variables seperated. I also recommend storing the subject in a variable.
$subject = 'Contact Form Submission'; // Place where we left of at Checkpoint.
$message = "Name: $name\n\nWebsite :$web\n\nComments:$body"; // Place where we left of at Checkpoint.
sendMail($email, $receiver, $subject, $message); // Place where we left of at Checkpoint.
You also have to make sure that you have access to a SMTP (Simple Mail Transport Protocol), so it might not be enough for you to be working localhost if you don't have a SMTP server to use. IF you have one, and It's not working, then you might have to config the php ini settings:
ini_set('SMTP' , 'smtp.yourwebsite.com');
ini_set('smtp_port' , '25');
ini_set('username' , 'example#example.com');
ini_set('password' , 'pass');
ini_set('sendmail_from' , 'example#example.com');
Note: It would be smart to actually make the modified mail function RETURN the mail function, so that you can check whether it was successful or not.

displaying name on next page once email is sent

I have a form which sends to my email. Once form is complete and submitted it redirects to a email sent conformation page. I want to display the name on this page grabbing it from the field in which they inserted their name.
EMAIL FORM PHP:
<?php
$your_email ='info#example.com';
session_start();
$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$user_message = $_POST['message'];
///------------Do Validations-------------
}
?>
header('Location: emailsent.php?name=' . $name);
In emailsent.php
echo $_GET['name'];
May I ask why are you using header('Location: emailsent.php');? instead of using header you can reload the same page and modify it to show a different content if the email was sent.
<?php
if(isset($_POST['submit']) && empty($errors)) {
//show a success message and some html code... e.g.:
echo 'Thank you, '.$_POST['name']; //this is the senders name
}
?>
If you must use headers, here is a solution

Categories