I got some help with an email form, and I feel that I am almost there as the script sends an email but need a few tweaks before I put the form up. Here is my code right now:
index.html:
<div id="main">
<form method="post" action="mailer.php">
<div id="text">
Please enter your email address.
</div>
<input type="text" name="q" id="search" />
<input type="submit" name="submit" id="submit" value="Go!" />
</form>
</div>
mailer.php:
<?php
$email = addcslashes($_REQUEST['q']) ;
if ($email==FALSE){
echo "You forgot to enter your email";
}
else
mail( "example#gmail.com", "E-Mail entered",
"E-Mail entered: $email");
header( "Location: http://www.example.com/thankyou.html" );
?>
A few issues I am running into:
The email being sent does not actually include the email entered, the email comes from Apache#ipaddress.ec2.internal and the Body is Email Entered:
which does not include the email string - is there something buggy with the code?
Also, my if statement doesn't seem to work. Even if I leave the box black, it still assumes a valid email address was sent.
Finally, is there a parameter that sees if the address is in the correct format? ie: includes the # the . and the domain?
Many thanks for any help!
First off, you can use filter_var($email, FILTER_VALIDATE_EMAIL) to test the submitted address. This function returns false if it's not valid. Second, mail() requires a 4th parameter to assign a return address in your message header. Here's an example:
mail(
'to#address.com',
'Subject',
'Message Body',
'From: from#address.com'
)
Regarding your if/else statement, test $_POST['q'] == NULL first, then change $email = addcslashes($_REQUEST['q']); to $email = str_replace(array('\'', '"'), '', $_POST['q']); - no real reason to escape characters in this case. Just take em' out.
Edit: This is what your code should look like:
$email = str_replace(array('\'', '"'), '', $_POST['q']);
$isValid = filter_var($email, FILTER_VALIDATE_EMAIL);
if ( $_POST['q'] == NULL ) {
echo "You forgot to enter your email";
} elseif ( $isValid == FALSE ) {
echo "Please enter a valid email address";
} else {
mail(
'example#gmail.com', // Your address that you want the message sent to
'Subject',
'Message Body',
'From: ' . $email // The address collected
);
header( "Location: http://www.example.com/thankyou.html" );
}
Does that make a little more sense?
Do note that because of modern spam filters, this method may not make it to every recipient. Creating useful email headers can be a bit of an art-form that takes some practice.
To change the "from field", try something like this:
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
As far as your if statement, check the $_REQUEST variable first:
if (isempty($_REQUEST['q']) { echo "forgot to enter email"; }
Finally, here's a link to a method to validate email addresses: http://www.linuxjournal.com/article/9585
Try this for checking if the email is set.
if(isset($_REQUEST['q'])) ..
As far as the "Email Sender". I believe you are looking for the "From" header.. that can be done like this..
$header = "From: Your Name <example#gmail.com>";
mail($to, $subject, $message, $header);
More details are available at http://us3.php.net/manual/en/function.mail.php
Related
my form is working as intended but for some reason the email will only send to one of my email accounts and not the other I am putting the right email in the email field so that isn't the issue however I can't seem to see where I am going wrong I assume it's because I'm using $email to grab the email address to where the 2nd email is suppose to go...here is my php where am I going wrong?
<?php
$from = 'Pixel Wars - Press Inquiry';
$to = "my-email#gmail.com, $email";
$subject = 'Press Inquiry from Pixelwars.com';
function errorHandler ($message) {
die(json_encode(array(
'type' => 'error',
'response' => $message
)));
}
function successHandler ($message) {
die(json_encode(array(
'type' => 'success',
'response' => $message
)));
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "Name: $name\r\n Email: $email\r\n\r\n Message:\r\n $message";
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message)) {
errorHandler('Header injection detected.');
}
// Check if name has been entered
if (!$_POST['name']) {
errorHandler('Please enter your name.');
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
errorHandler('Please enter a valid email address.');
}
// Check if message has been entered
if (!$_POST['message']) {
errorHandler('Please enter your message.');
}
// prepare headers
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/plain; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$email>" . PHP_EOL;
$headers .= "Return-Path: $to" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
// send the email
$result = #mail($to, $subject, $body . "\r\n\n" .'------------------ '. "\r\n\n" .'Hello '.$name.' we will contact you as soon as possible about your query.' ."\n". 'Dont forget to keep visiting www.pixelwars.com for more updates and awesome content.' ."\n". 'We will email you back on the provided email below, thank you and have a nice day.' . "\r\n\n" .'-- '.$email, $headers);
if ($result) {
successHandler('Thank You! we will be in touch');
} else {
errorHandler('Sorry there was an error sending your message.');
}
} else {
errorHandler('Allowed only XMLHttpRequest.');
}
?>
Thank you in advance if anyone can crack it
You don't have $email assigned when you are defining $to so your second address is not set.
Demo: https://3v4l.org/QIIJu
Solution, move the $to assignment to later in the script. Also use error reporting, this would have thrown an undefined variable notice.
e.g.
<?php
$from = 'Pixel Wars - Press Inquiry';
$subject = 'Press Inquiry from Pixelwars.com';
....
$to = "my-email#gmail.com, $email";
$result = #mail($to, $subject, $body ....
because at this point the $email is defined. Also don't use error suppression, that is just hiding useful information. If you don't want it displayed hide the error displaying but still log them.
You need to add the multiple email address in $to
$to = "address#one.com, address#two.com, address#three.com"
Needs to be a comma delimited list of email adrresses.
mail($email_to, $email_subject, $thankyou);
Thanks
I have written a php mail function to allow a user on my website to fill in a form and send the form to my email. as the question says the email is working once the user send the form however it only appear in my junk email folder instead, i am not a php developer but after doing some research i have noticed a lot people mentione about PHPMailer which i never heard of or used before.
i would much appreciate with a bit oh help.
$to="myemail.com";
//Errors
$nameError="";
$emailError="";
$errMsg="";
$errors="";//counting errors
$name="";
$email="";
$message="";
if(isset($_POST['send'])){
if(empty($_POST['yourname'])){ //name field empty
$nameError="Please enter your name";
$errors++; // increament errors
}else{
$name= UserInput($_POST['yourname']);
if(!preg_match("/^[a-zA-Z ]*$/", $name)){
$nameError="Only letters and white space accepted";
$errors++;
}
}
if(empty($_POST['email'])){
$emailError="Enter email";
$errors++;
}else{
$email = UserInput($_POST['email']);
if(!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)){
$emailError="Invalid Email";
$errors++;
}
}
if(empty($_POST['msg'])){
$errMsg="Enter message";
$errors++;
}else{
$message=UserInput($_POST['msg']);
}
if($errors <=0){//No errors lets setup our email and send it
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <' . $email . '>' . "\r\n";
$text = "<p>New Message from $name </p>";
$text .= "<p>Name : $name</p>";
$text .= "<p>Email : $email</p>";
$text .= "<p>Message : $message</p>";
mail($to, "Website Contact", $text, $headers);
$success="Thank your message was submitted";
$_POST= array(); //clearing inputs fields after success
}
}
//Filter user input
function UserInput($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Your using the email from their posted variable in your header. When your email server receives this it's going to look like it is spoofed because it is. Your site isn't going to be one of the mail servers setup for that domain.
When setting up MX and DNS records for email you use SPF or key signing to prove who sent the message and that it came from a trusted mail server for that domain. You may want to change the from to be an email and domain you control. You are getting their email in the body anyway.
Worst case if you don't have control over SPF records you could at least mark the from email, assuming it is something you control, as always trusted so it wouldn't go into your junk mail.
$headers .= 'From: <' . $to . '>' . "\r\n";
I'm just getting into coding and put together a webpage using dreamweaver. I created a php page with my email coding which is executed from a form and submit button on an html page. I continously recieve blank emails on a daily basis which apparently means I need to add validation coding. I tried adding the coding but the problem persists. The page still submits even if the form is blank.
Below is my current coding.
<?php
if(!filter_var($_POST['CustomerEmail'], FILTER_VALIDATE_EMAIL)) { $valerrors[] = "The email address you supplied is invalid." ;}
$customeremail=$_POST['CustomerEmail'];
$email='test#gmail.com';
$custfirst=$_POST['CustomerFirstName'];
$custlast=$_POST['CustomerLastName'];
$custphone=$_POST['CustomerNumber'];
$custaddress=$_POST['CustomerAddress'];
$custcity=$_POST['CustomerCity'];
$custstate=$_POST['CustomerState'];
$custrequest=$_POST['CustomerService'];
$subject='Service Request';
$body = <<<EOD
<br><hr><br>
Email: $customeremail <br>
First Name: $custfirst <br>
Last Name: $custlast <br>
Phone: $custphone <br>
Address: $custaddress <br>
City: $custcity <br>
State: $custstate <br>
Request: $custrequest <br>
EOD;
$headers = "From: $customeremail\r\n";
$headers .= "Content-type: text/html\r\n";
$Send = mail($email, $subject, $body, $headers);
$confirmation = <<<EOD
<html>"Thanks for Submitting."</html>
EOD;
echo"$confirmation";
?>
It's possible that I'm placing the if statement in the wrong place. Can someone correct my coding so the confirmation page will not load and the email will not be sent if the customer email is left blank?
You need to check like this
if (filter_var('abc#gmail.com', FILTER_VALIDATE_EMAIL)) {
echo 'VALID';
} else {
echo 'NOT VALID';
}
see here : http://php.net/manual/en/function.filter-var.php
You could initialize $valerrors = []; at the beginning and use a condition such as:
if (empty($valerrors)) {
$headers = "From: $customeremail\r\n";
$headers .= "Content-type: text/html\r\n";
$Send = mail($email, $subject, $body, $headers);
$confirmation = <<<EOD
<html>"Thanks for Submitting."</html>
EOD;
}
Note that you should apply similar validation to all user inputs.
I think u can add "required" attribute to your email input in your form Html
<input type="email" name="CustomerEmail" required>
So Html wont allow you to submit until you provide a value
Im new to this PHP stuff so please excuse my ignorance
Im after having just one input text box in my flash website where a person just enters there email address and at the click of a button it sends an email to me to a pre defined email address with a predefined subject heading and the email address that was entered in the body of the email
Anyone know of any links or can give some help
all the ones i have found want names email subject message and so on
Any help is appreciated
Mark
EDIT
ok I have the following
In flash I have an input text converted to a movieclip called "addy". Inside the movie clip which has the inputbox which has the variable name "emailaddy"
A Button called "email"
The code i Have running when "email" is clicked is
on (release) {
form.loadVariables("email.php", "POST");
}
the email.php script is as follows
<?php
$sendTo = "mark#here.co.uk";
$subject = "Subscribe to Website";
$headers = "From: Website";
$headers .= "<" . $_POST["addy"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["addy"] . "\r\n";
$headers .= "Return-Path: " . $_POST["addy"];
$message = "Please Subscribe me to Website";
mail(recipient, subject, message, other headers);
mail($sendTo, $subject, $message, $headers);
?>
when I click the button nothing happens
what im after is when the button is clicked for and email to be sent in the following format
To: "mark#here.co.uk"
From: email address specified in text field "addy"
Subject: "Subscribe to Website";
body: "Please subscribe me to Website"
Your help is greatly appreciated
mark
The following code might help:
<?php
$user_mail=$_POST["mail"]; //or $user_mail=$_GET["mail"]; Set to your convenience!
$to_mail="abcde#xyz.com"; //Change to your email address
$message="New user's Email: ".$user_mail; //Change to your requirements
$subject="New user registered"; //Change to your preferred subject
$from="registration#yourwebsite.com"; //Change to your website mail id
mail($to_mail,$subject,$message,"From: $from\n");
To know more about the mail function, please see the documentation.
Well in depth you can do like this
<?php
if($_POST){
$userEmail = $_POST0["emailaddy"]; // textbox variable name comes here
$to = 'abc#xyz.com'; //write down here your email
$subject = 'Subscribe to website'; // your subject goes here
$message = 'Please Subscribe me to Website'; // Mail body message
$headers = 'From: ' . $userEmail . "\r\n" .
'Reply-To: ' . $userEmail . "\r\n" .
'Return-Path: ' . $userEmail; //can send x-Mailer also
mail($to, $subject, $message, $headers);
}else{
echo "Invalid Request";
return false;
}
Don't forget to check first $_POST is happened or not. No need to send Return-path instead of this use x-Mailer which sound good for other mail service providers.
To know more about this read documentation here.
If you want to connect to an SMTP server ,like Postfix or gmail there is a neat php library called PhpMailer.
It is well documentated, so you should be good to go by googleing it :)
This question already has an answer here:
Extra field in this contact form [closed]
(1 answer)
Closed 9 years ago.
I tried asking this before but I think I made things too complicated and nobody answered. This is take two. I need to add a phone number field to this line of PHP. I have NO IDEA how to add it.
mail( "contact#jeremyblaze.com", "Contact Form: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
I've tried this, but the email never goes through.
mail( "contact#jeremyblaze.com", "Contact Form: ".$_POST['name'], $_POST['phone'], $_POST['text'], "From:" . $_POST['email'] );
Here's the full PHP if you need it.
<?php
if ( isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
mail( "contact#jeremyblaze.com", "Contact Form: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
}
?>
Thanks :)
Your code should be like this
Your are doing wrong way.Add proper subject and message.Please study this link for details.
$subject = 'the subject';
$message = 'Your subject and add phone no here';
mail('contact#jeremyblaze.com', $subject, $message);
Here is the php mail function page:
http://php.net/manual/en/function.mail.php
You cannot add the phone number inside the function like that.
Here is the basic function:
mail(email,subject,body);
You need to add the phone number to the body of the text:
$email = "contact#jeremyblaze.com";
$subject = "Contact Form: ".$_POST['name'];
$body = "From: ".$_POST['email']."\n\r\n\rPhone: ".$_POST['phone']."\n\r\n\r".$_POST['text'];
mail($email,$subject,$body);
You should really look at the documentation on the php site:
http://php.net/manual/en/function.mail.php
It will show you how to set additional headers for reply-to and from, etc. But what I gave you should make what you are trying to do.
Just to complement #Mahmood Rehman info, if you want send a HTML e-mail you can do this:
// From
$email_from = $vEmail;
// To
$to = 'contact#jeremyblaze.com';
// Subject
$subject = $vSubject;
// HTML Message
$message = "<html>
<head>
<title>Title</title>
</head>
<p><b>$vName</b> send the follow message:</p>
<p><b>Subject:</b> $vSubject</p>
<p><b>Message:</b> $vMessage </p>
<p><b>E-mail:</b> $vEmail </p>
<p><b>Phone:</b> $vPhone </p>
</body>
</html>";
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: $email_from\r\n";
// Mail it
mail($to, "=?utf-8?B?".base64_encode($subject)."?=", $message, $headers);