This might seem like such an amateur thing, but I am an amateur when it comes to php...
Basically a website I'm creating for an organisation I'm a part of needs a contact form, because I'm always trying to learn I'd prefer to have made the code myself rather than just get some plugin. However my code doesn't quite seem to work.
Also first post, I'm pretty sure this isn't a duplicate due to it being different code, I have been using posts on this site to try get it to work but no luck, so thought best to post my code and hopefully people with more brain cells than me can help
Here's the html for the page itself:
<form method="post" action="contactengine.php">
<label for="Name"><h2>Your Name:</h2></label>
<input type="text" name="Name" id="Name" placeholder="Please Enter Your Name"/>
<label for="Email"><h2>Your Email:</h2></label>
<input type="email" name="Email" id="Email" placeholder="Please Enter Your Email Address"/>
<label for="Message"><h2>Message:</h2></label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
The PHP:
<?php
$EmailFrom = "webmaster#temporary.co.uk";
$EmailTo = "webmaster#temporary.co.uk";
$Subject = "Contact Form Submission";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// 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: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
I feel quite stupid because there's loads of literature on creating working contact forms but none of them seem to work
I'm hosted on 1and1 if that makes any difference?
Thanks in advance
EDIT: Okay I forgot to mention the problem: emails just aren't delivering, so I don't actually know if success is working or not, I've tried different email addresses, checked spam the whole shabang - so I'm struggling to work out what's going wrong on it
Don't use the built-in mail command. You are going to want to use something like Swift Mailer as a mail() replacement, and a mailing service like mailgun.org to send mails.
The built-in mail command may be blocked on the server. Or, since you're not sending from a server that is designed to send emails, the emails may be blocked at the SMTP level by someone else's blacklist. Or maybe they're in your spam box.
You may want to install MailHog on your server and use that as an SMTP server to test whether the mail() command even works on the server.
Related
I'm trying to achieve something without success.
I have a contact form with two fields, one is an input for the email address and another is a textarea. I'm trying to convert this text included into an encoded text and print it via mail to the mail included on that same form.
for example, here's my form.
HTML
<form action="email.php" method="post" class="form" id="form1">
<input type="text" name="email" placeholder="Email...">
<textarea class="textarea" name="clientMessage" placeholder="Write them something secret here, they won't know who wrote it" maxlength="120"></textarea>
<input type="submit" class="sendButton" id="sendButton" value="Send">
</form>
So now I'll try to print this "clientMessage" on the $message .=""; of my mailer.php to send it to the mail introduced on the form. So far I receive the mail but with the code as text. Not sure what I'm doing wrong:
mailer.php
<?php
$clientMessage= $_POST["clientMessage"];
echo base64_encode($clientMessage);
$message = '<html><body>';
$message .= "echo base64_encode($clientMessage);";
$message .= "</body></html>";
$sender= $_POST["email"];
$to = $sender;
$subject = 'Something for you';
$from = 'secret#noreply.com';
$fromEmail = 'secret#noreply.com';
$header = 'From: ' . $from . '<' . $fromEmail . '>';
mail($to, $subject, $message, $headers);
header("Location: https://www.website.com/thanks.html");
exit;
?>
Someone can tell me what I'm doing wrong:)? thanks!
You're already in PHP, so the output of $message .= "echo base64_encode($clientMessage);" will be that string. Just try $message .= base64_encode($clientMessage);
I hope I understood correctly what you meant.
The solution I found was this one
PHP
$clientMessage= $_POST["clientMessage"];
$encodedMessage = "https://www.address.com/read.html?m=";
$encodedMessage .= base64_encode($clientMessage);
First of all posting the clientMessage from the form, then creating the link with another variable created used as a link, first of all the address and the termination of the encoded text on the second one. works just fine!
i've been trying to fix this one with solutions i've found here and in other foruns but nothing seems to work. I'm really noob with PHP. I don't know if this info is relevant but i'm hosting my project at byethost.com which they say it supports PHP.
I have made a contact form in my HTML contact page, made a PHP file to process the data and send it to my gmail adress. It says message successfully sent, but the data never arrives at my inbox on gmail. I've tried changing the PHP code (with copy paste solutions) in alternative ways but with no luck. I've also tried to sent it to my hotmail account but it doesn't work either. Can anyone help me?
Here is the HTML contact form:
<div id="form_wrap">
<form method="post" action="form_process.php" id="contact_form" class="contact">
<label>
<span>NOME*</span>
<input name="nome" type="text" tabindex="1" required>
</label>
<label>
<span>E-MAIL*</span>
<input name="email" type="email" tabindex="2" required>
</label>
<label>
<span>TELEFONE*</span>
<input name="phone" type="tel" tabindex="3" required>
</label>
<label>
<span>WEBSITE</span>
<input name="website" placeholder="http://" type="url" tabindex="4">
</label>
<label>
<span>MOTIVO DE CONTACTO*</span>
<select class="escolha" name="motivo" size="1" tabindex="5" required>
<option>Contratá-lo</option>
<option>Fazer uma pergunta</option>
<option>Dizer olá ou agradecer</option>
</select>
</label>
<div>
<label>
<span>MENSAGEM*</span>
<textarea name="message" tabindex="6" required></textarea>
</label>
</div>
<div>
<input name="submit" type="submit" value="Enviar" id="submit">
</div>
</form>
<a class="subtop" href="#subtop">– Voltar ao topo</a>
</div>
Here is my form_process.php:
<?php
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];
$to = "mygmailadress#gmail.com";
$subject = "Site contact form";
$header = "From: ".$fromText."\r\n";
$header .= "Cc: ".$ccText."\n";
$header .= "Reply-To : ".$fromText."\r\n";
$header .= "Return-Path : ".$fromText."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
mail($to, $subject, $header, $message, "From: " . $nome);
}
if(#mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
?>
Why twice mail function?
mail has 4 parameters, you give 5 params (from is a paert of fourth, it should be in $header)
variable $emailRecipient used in your mail function doesn't exists.
in 'from' header should be an e-mail address (and name), not only a non-mail string
There are many possible reasons that the email never arrives.
It could be caught in spam, blocked by your host or your mail function could be incorrectly setup in your php.ini file.
However, from your code it looks like you are using the mail() function incorrectly.
You are trying to send the email twice by calling the function twice. Just call it once like:
if(mail(...)) {
echo 'good times';
} else {
echo 'boo, no email was sent';
}
Secondly you are using the function incorrectly. According to the documentation here the mail function takes five arguments like so:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$additional_headers and $additional_parameters are optional as denoted by the [square brackets]. $to, $subject and $message are required in that order.
Thirdly I heavily suggest NOT using the built in mail() function.
I suggest using SwiftMailer. It's a fully comprehensive PHP library which will look after you.
U are trying to send mail twice you are using wrong variable names.
This code works for me.
<?php
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];
$to = "asdf#gmail.com";
$subject = "Site contact form";
$header = "From: ".$email."\r\n";
$header .= "Cc: ".$email."\n";
$header .= "Reply-To : ".$email."\r\n";
$header .= "Return-Path : ".$email."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
if(mail($to, $subject, $message, $header))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
}
?>
If you are usinf free hosting, they probably restrict your ability to send email.
Something like that is happening:
https://www.freehosting.com/client/knowledgebase.php?action=displayarticle&id=25
PHP mailing functionality is limited on free account to prevent abuse. To make it working, your mailing script should be configured to use SMTP server 'cpanel.freehosting.com' and to authenticate against it using credentials of email account set up in cpanel.
Paid accounts are provided with unrestricted access to PHP mailing functionality.
You can find more info on setting up email authentication in PHP scripts at this link:
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
Optionally, PHP mail() can be enabled by purchasing corresponding addon HERE.
Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
<?php
$errors = '';
$myemail = 'EMAIL#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
after that you can addded your futher code
Try add in top file:
error_reporting(E_ALL);
and edit your code, see:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
http://www.php.net/manual/en/function.error-reporting.php
http://www.php.net/errorfunc
http://php.net/manual/pt_BR/function.set-error-handler.php
http://www.php.net/register_shutdown_function
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
The problem is with your From field in your $headers variable.
you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.
The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.
I've already mentioned it in details here.
I im trying to redirect to my homepage after submitting a message on my contact form, the form sends the email but I get this message:
Array
(
[name] => Abdo
[company] => Mediabyrån A&B
[email] => a.el-madhoun#hotmail.com
[content] => Hejsan
[contact_to] => info#web.se
)
Warning: Cannot modify header information - headers already sent by
(output started at
/customers/4/5/a/webelite.se/httpd.www/kontakt.php:3) in
/customers/4/5/a/webelite.se/httpd.www/kontakt.php on line 39
My contact form;
<form action="kontakt.php" method="post">
<p><input type="text" required="required" id="name" name="name" class="text_input" size="22" />
<label for="name">Namn *</label></p>
<p><input type="text" required="required" id="company" name="company" class="text_input" size="22" />
<label for="company">Företag *</label></p>
<p><input type="email" required="required" id="email" name="email" class="text_input" size="22" />
<label for="email">Epost *</label></p>
<p><textarea required="required" name="content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="info#web.se" name="contact_to"/>
</form>
and this is my PHP:
<?php
echo $name = $_POST['name'];
echo $company = $_POST['company'];
echo $email = $_POST['email'];
echo $content = $_POST['content'];
$mail_to = 'info#webelite.se';
$subject = 'Lilla form'.$name;
$body_message = 'From: '. $name . "\n";
$body_message .= 'company: '. $company . "\n";
$body_message .= 'E-mail: '. $email ."\n";
$body_message .= 'Message: '. $content;
$headers = 'From: '. $mail_name . "\r\n";
$headers .= 'Reply-To: '. $email ."\r\n";
$success = mail($mail_to, $subject, $body_message, $headers);
echo "<pre>";
print_r($_POST);
header('Location:mydomain');
?>
I also tried using if
($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">";
This worked but I got a ugly half a second flash between hitting submit and being redirected.
All help is appriciated.
Thank you
You can't output anything to the screen before redirecting.
Remove all your echo'es and print_r's and you should be fine.
EDIT:
Also as #jhonraymos mentioned, be sure to use header() properly. Maybe you changed this to hide your actual page you're redirecting to. Either add a local file with correct path definitions or if redirecting to an other domain, you need to define the full url. See Uniform resource locator if in doubt.
Another EDIT:
I see you updated your question. Stop trying indian magic, such as
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">";
Just accept the fact not to output ANYTHING to the screen before headers() and your soul is safe forever. This is not the place to mix http-meta in. PHP can do this just fine.
This might sound as a limitation first, but believe me, it isn't. It's a blessing.
Headers are used to communicate with your client's browser. They're like little commands that the browser will perform when received. When you output any data (text,numbers,whatever), you're client's browser will print that data. After that, the browser will no longer be interested in any headers you send.
The header() function is a function used to send custom headers. So when this function is called, headers are sent out to your client's browser.
Now you have a very brief understanding of what it is you're actually trying to do, you should be able to see where your problem lies.
You are outputting other data before sending those custom headers. This is what's triggering the error.
So this:
echo "<pre>";
print_r($_POST);
Should not be before this:
header('Location:mydomain');
this is error
print_r($_POST);
header('Location:mydomain');
You are printing something before the header("location: mydomain.com")
yes, you are not supposed to echo, print anything before header
try placing this at the top of your page:
<?php ob_start(); ?>
then at the bottom of the page place :
<?php ob_end_flush(); ?>
What the
echo "<pre>";
print_r($_POST);
is for ?
Your redirection may happen before any echo function is called, no ?
This is very common problem in php. some ways to sort out:
use on top of the page.
check if you accidentally add some white spaces before the php open tag or after the php closed tag.
if still not solve, use java-script window.location instead of header.
Hope it help.
Happy coding!!
You get this warning because you output to the screen your variables' values and redirecting with header after that.
Headers cannot be sent after your print something (echo , print_r ...).
In order to fix it , follow the next code:
$name = $_POST['name']; //no echo
$company = $_POST['company'];//no echo
$email = $_POST['email'];//no echo
$content = $_POST['content'];//no echo
$mail_to = 'info#webelite.se';
$subject = 'Lilla form'.$name;
$body_message = 'From: '. $name . "\n";
$body_message .= 'company: '. $company . "\n";
$body_message .= 'E-mail: '. $email ."\n";
$body_message .= 'Message: '. $content;
$headers = 'From: '. $mail_name . "\r\n";
$headers .= 'Reply-To: '. $email ."\r\n";
$success = mail($mail_to, $subject, $body_message, $headers);
//echo "<pre>";
//print_r($_POST);
I would like to be able to send mail from my homepage but it aint working as I want. i get a mail but it doesnt say from home. Just says Unknown. The thing I have this in two diffrent places. The other place I use other textareas and there I get everything in my mail and it works fine but this other place I am trying to work I only want them to add there email and then it should be sent with all info.
My code on my page:
<form id="subscribe" class="clearfix" method="post" action="get_mail.php">
<div class="field alignleft">
<input type="text" value="Enter your email" onclick="if(this.value=='Enter your email')this.value='';" onblur="if(this.value=='')this.value='Enter your email';" />
</div>
<div class="search-btn">
<input type="submit" value="" />
</div>
</form>
Then I have this php script
<?php
//-----------------------------------------------------
//-----------------------------------------------------
$address= "xxxxx.xxxxx#outlook.com";
//-----------------------------------------------------
//-----------------------------------------------------
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$website = $_REQUEST["website"];
$subject .= "You have an email from your web site (from $name)! \n\n";
$message_content = strip_tags($_REQUEST["message"]);
$headers = "From: $name <$email>\n";
$headers .= "Reply-To: $subject <$email>\n";
$message = "--$mime_boundary\n\n";
$message .= "You have an email from your web site: \n\n\n";
$message .= "Name: $name \n\n";
$message .= "Email: $email \n\n";
$message .= "Website: $website \n\n";
$message .= "Message: $message_content \n\n";
$message .= "--$mime_boundary--\n\n";
$mail_sent = mail($address, $subject, $message, $headers);
echo $mail_sent ? "Success, mail sent!" : "Mail failed";
?>
Two things:
Try replacing "\n" in $headers with "\r\n".
As far as I know, some hosts (I think GoDaddy or Hostgator do this for example) will override the "from" value in sent emails and change it to the one you have with them. This means that if you don't explicitly have "from#domain.com" in your hosting account you won't be able to send emails from that address and it will be always overridden. You have to contact your host to check this. I suggest also checking the script on another server if possible.
None of your form fields appear to have names - your code here:
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$website = $_REQUEST["website"];
requires form fields with the names "name", "email", and "website". For example, your "email" field needs to look like this:
<input type="text" name="email" ... />