PHP undefined index error? - php

I have a simple contact form thats throwing up an
"Notice: Undefined index: e in \nas43ent\Domains\m\mysite.co.uk\user\htdocs\contact-us.php on line 24"
error, I cant seem to see why however as my syntax looks correct?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "dave#.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Subject: $subject,
Message: $message ";
//end of message
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Website Enquiry";
if (isset($_POST['name'])) {
// now lets send the email.
mail($to, $subject, $message, $headers);
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');
} else {header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');}
?>

Looks like it is related to e get parameter. After sending email you redirect user to some url like http://yourhost/com/somepage?e=Error+text
So the notice you get seems to be related to the place you display status message. You should put check there (as by default you don't have e in your query string):
if (isset($_GET['e'])) {
//output message
}

In place where you read $_GET['e'] paramter you should first check if it's present with isset:
if (isset($_GET['e'])) {
// show the message to user
}

This is not an error but a NOTICE this is really different.
If $_SERVER['HTTP_REFERER'] is not set, you will receive this notice.
You should add if(isset($_SERVER['HTTP_REFERER'])).
Try the following
if (isset($_POST['name']) AND isset($_SERVER['HTTP_REFERER'])) {

Related

php-html email with variable

Hallo I've got a problem with sending html E-mails with a dyanamic Link via PHP.
That is my PHP Code:
$empfaenger = $_POST['email'];
$test = sha1($_POST['email']);
$betreff = "Test Mail";
$from = "From: Peter <tets#gmail.com>\n";
$from .= "Reply-To: test16#gmail.com\n";
$from .= "Content-Type: text/html\n";
$text = "Please click <p><a href='http://defaultpage/default.php?res=".$test."'>here</a></p>.";
mail($empfaenger, $betreff, $text, $from);
In the mail Log all looks normal like the mail is sended, but I didn't receive one. In PHP error Log also nothing. If I set $test to a specified string it's working as expected, but not with the dyanamic variable $test. I hope that someone can help me becuse I can't get rid of this problem.

Wordpress not recieving contact form email

I have a website I'm working on with a contact form. Simple Name, Email, and Message fields. However, I'm not receiving the email from the form; though I do get the "Message Sent" success option.
I had found a way to test wp_mail to see if that was it, using https://gist.github.com/butlerblog/5c9b805529c419b81447#file-test_wp_mail-php and that works just fine - so I have no clue what the problem is.
<?php
//loading wordpress functions
require( '../../../wp-load.php' );
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$to = get_option('admin_email'); //Enter your e-mail here.
$subject = get_theme_option(tk_theme_name.'_contact_contact_subject');
$from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');
$body = "You received e-mail from ".$from." [".$name."] "." using ".$sitename."\n\n\n".$message;
$send = wp_mail($to, $subject, $body, $headers) ;
if($send){
wp_redirect($return.'?sent=success');
}else{
wp_redirect($return.'?sent=error');
}
?>
As I said, it does hit
wp_redirect($return.'?sent=success')
so I have no clue what is wrong.
EDIT:
Did a variable dump
To: uriah.h.brown#gmail.com
Subject: E-Mail from HollyBrown.Net
From: TestName
Name: test#yahoo.com
Message: TestMessage
Headers: From: test#yahoo.com Reply-To: E-Mail from HollyBrown.Net
Return: http://www.hollybrown.net/contact/
Sitename: The Artwork of Holly Brown
Body: You received e-mail from TestName [test#yahoo.com] using The Artwork of Holly Brown TestMessage
Send: 1
Warning: Cannot modify header information - headers already sent by (output started at /home/panoramicpanda/hollybrown.net/wp-content/themes/widely/sendcontact.php:21) in /home/panoramicpanda/hollybrown.net/wp-includes/pluggable.php on line 1178
The header already sent error leads me to believe output has already been started before you try and send the headers. In this case, the redirect won't work. Try this instead:
/loading wordpress functions
require( '../../../wp-load.php' );
ob_start(); // Starts data buffer.
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$to = get_option('admin_email'); //Enter your e-mail here.
$subject = get_theme_option(tk_theme_name.'_contact_contact_subject');
$from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');
$body = "You received e-mail from ".$from." [".$name."] "." using ".$sitename."\n\n\n".$message;
$send = wp_mail($to, $subject, $body, $headers) ;
ob_end_clean();// Purges data in buffer.
if($send){
wp_redirect($return.'?sent=success');
}else{
wp_redirect($return.'?sent=error');
}
Notice the ob_start and ob_end_clean. This will throw your output into the "buffer" and output it clean. The reason this is a problem, is because if any markup is sent BEFORE your sendmail (ie: html generated through templates etc, then the headers have already been started. At that point, unless your server configuration specifically allows you to modify already sent headers, then you will get the error you are getting. Using output buffering allows you to send the data as you intend.

Hyperlink not getting displayed in Php mail

I'm trying to email a link thru PHPMail. Somehow link is showing as plain text in email.
I want that link should be clickable when received in email. Currently it is showing as plain code.
Below is the code, please help!
<?php
$name=$_POST['firstname'];
$email=$_POST['email'];
$phoneno=$_POST['phoneno'];
$com=$_POST['comment'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
?>
Print Boss your receipt
<?php
//$link="hello.php?a=$name&b=$email&c=$phoneno&d=$com";
$to = "ketanjoshi1993#gmail.com";
//$from = $email;
$subject = "send email in pdf format";
$message = "<a href='#'>test</a>";
$email = mail($to,$subject,$message);
if( $email == true )
{
echo "sent";
}
else
{
echo "not sent";
}
?>
You forgot to use your headers in your mail call:
$email = mail($to, $subject, $message, $headers);
So you forget to add the headers
try adding it as
$email = mail($to,$subject,$message, $headers);
It will work
You forget to add the headers
You can do like this :
$email = mail($to,$subject,$message, $headers);
If you don't pass the header parameter in PHP mail() function , default headers will be set & your will be treated as simple text body.

Can you insert an if statement in php email

// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your#email.com';
$body = HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: thanks.html');
The question is, weather or not you can insert a php if statement inside the html email body or the best way to add it in there. I'm creating a detailed inventory and once submitted, it needs to be email to an email. But I don't want to email the full Inventory (including empty inputs), It should only email the fields that have something other than 0. I was thinking:
if ($armChair > 0) { echo 'Arm Chairs: ' . $_POST['armChair']; } ]
But it doesn't seem to actually work... any ideas?
This is a badly constructed question.
Yes, you can definitely do that but you need to do it in a way that is sane. We can't tell by what you posted here since you just plugged in "HTML" where the body is defined.
$body = "Hi there,\r\n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."\r\n" : "";
$body.= "some more text";
If you mean overwrite a segment of $message, yes you can do that as well using something like machine tags {INVENTORY} or the likes...
$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."\r\n", $message) : "";
which of course requires the string {INVENTORY} somewhere in your $message var

PHP Contact Form not Sending?

Ive got a contact form that isnt sending but is outputting that the message is sent? Can anybody see a problem?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#email.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Subject: $subject,
Message: $message ";
//end of message
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Website Enquiry";
if (isset($_POST['name'])) {
// now lets send the email.
mail($to, $subject, $message, $headers);
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');
} else {
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');
}
?>
The "From" header should have a syntactically correct email address. You also need to check the return value of the "mail" function.
$header .= "From: Website Enquiry <enquiry#website.com>";
PS: Please improve your code formatting.
Try to enter an email at From: in $headers.
Like $headers .= "From: youremail#provider.com" or
$headers .= "From: Website Enquiry <youremail#provider.com>"
And you should change it to
if(mail(...)) {
//success
}
else {
//email failed
}

Categories