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.
Related
So I've completed a contact form made in PHP. After testing it sends emails, but in the email header it is not capturing the sender's name or email address. It shows up as sh-908129268#eu.hosting-webspace.io
Is there a way to correct that? I need to be able to respond to emails.
The inputs are as follows:
<form id="contact" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<input class="input is-small" type="text" name="name" placeholder="Text input" value="<?= $name ?>" >
<p class="help is-danger"><?= $name_error ?></p>
<input class="input" type="email" name="email" placeholder="Email Address" value="<?= $email?>">
<p class="help is-danger"><?= $email_error?></p>
// back-end file
$sendTo = 'test#email.com';
$subject = 'Business Enquiry';
$headers = "";
$headers .= "Sent From: ".$name. "\r\n";
$headers .= "Reply To: ".$email. "\r\n";
$headers .= "Message: ".$message. "\r\n";
if (mail($sendTo, $subject, $message, $headers)) {
$success = "Thank you for your message, I will reply as soon as I can.";
$name = $email = $message = ''; // resets all fields
}
You cannot pick the names of the headers freely, you have to adhere to the standard (RFC 5322 Internet Message Format).
Your header "Sent From" should be just From, your header "Reply To" should be Reply-To and your header "Message" shouldn't be in the headers at all - you already pass the message as such into the mail function.
All fixed now, thanks. All I needed was to specify one header ,
$headers = "From: ".$email;
So it's just $subject and $headers
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have an HTML form that posts to a PHP mail function. For some reason it does not send the content anymore. The emails display "This email has no content." Am I missing something?
//HTML - Post method form.
<form class="form" id="form" action="submitform.php" method="post">
<h2 style="padding-top: 10px;">Contact Us</h2>
<input class="input" type="text" placeholder="Name" name="name"><br>
<input class="input" type="text" placeholder="E-Mail Address" name="email"><br>
<input class="input" type="text" placeholder="Phone #" name="phone"><br>
<textarea class="input" placeholder="Questions, Specifications, etc."
name="message</textarea>
<input class="inputButton" type="submit">
</form>
//PHP - Gets posted HTML input data and sends it to the email, formatted with \n
<?php
$to = 'example#example.com' ;
$subject = 'Inquiry' ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nPhone: $phone \nMessage: $message \n";
$sent = mail ($to, $subject, $message) ;
if($sent == true) {
echo "<h3>Thank you for your inquiry.</h3>";
}else{
echo "<h3>Sorry, your message wasn't sent.</h3>;
}
?>
I've found defining headers, even if I just want default values, helps me get my mail delivered. I can't say this is what you need to do because your code looks like it should run successfully as is.
An example of setting the headers to (I believe) default values:
$to = $to_email;
$subject = $your_subject;
$message = $your_message;
$headers = "From: S.O.<no-reply#dontusethis.email>\r\n" .
"Reply-To: [can be nice, not needed of course]\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Mail not being received by hotmail.com
I have this simple form on my site and I do not receive emails when it sends into my Hotmail account, not even the Junk folder.
Here is the form code:
<form action="mail.php" method="POST">
<p><label title="Name">Name:</label><br />
<input type="text" name="name" autocomplete="on" required="required"></p>
<p><label title="Email">Email:</label><br />
<input type="text" name="email" autocomplete="on" required="required"></p>
<p><label title="About">My message is about...</label><br />
<select name="about">
<option value="general">General Query</option>
<option value="wedding">Wedding</option>
<option value="corporate">Corporate Event or Trade Show</option>
<option value="other">Other Event</option>
</select>
<p><label title="Message">Message:</label><br />
<textarea name="message" rows="6" cols="25" required="required"></textarea></p>
<input type="submit" value="Send">
</form>
And the mail.php file:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$about = $_POST['about'];
$formcontent="From: $name \n About: $about \n Message: $message";
$recipient = "MyEmailAddress#Live.co.uk";
$subject = "Contact Form";
$mailheader = "Reply-To: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
I do end up seeing a page with "Thank you!" displayed but no email is received.
Mail delivery is a tricky business... just because you send mail does not mean that anyone will receive it. Many receiving servers will simply ignore the incoming message if it does not meet certain criteria (in my experience Gmail and Hotmail are particularly prone to just silently denying delivery, so it doesn't even end up in SPAM). There are a few things to make sure you've done:
1) You've set up PTR/SPF (reverse lookup) entries in your DNS records
2) Make sure that you're not on any blacklists (http://www.mxtoolbox.com/blacklists.aspx)
3) Expand your headers
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: $contentType; charset=utf-8\r\n"
."Content-Transfer-Encoding: 8bit\r\n"
."From: =?UTF-8?B?". base64_encode("Your sending display name") ."?= <$from>\r\n"
."Reply-To: $replyTo\r\n"
."X-Mailer: PHP/". phpversion();
However, if you really want to ensure that mail gets through, send mail via SMTP. You can never guarantee mail delivery, but it will be much more reliable. If you're not sending a large volume of mail, you might try using Mandrill or similar service to relay emails for you.
You can use the following method. returns true on success.
function sendMail($email, $subject, $message)
{
$supportEmail = 'info#abc.com';
$from = 'Abc';
$msg = $message;
$from = str_replace(' ', '-', $from);
$frm = $from.' <'.$supportEmail.'>';
preg_match("<(.*)#(.*\..*)>", $frm, $match);
///////////////////Headers/////////////////
$hdr='';
$hdr.='MIME-Version: 1.0'."\n";
$hdr.='content-type: text/html; charset=iso-8859-1'."\n";
$hdr.="From: {$frm}\n";
$hdr.="Reply-To: {$frm}\n";
$hdr.="Message-ID: <".time()."#{$match[2]}>\n";
$hdr.='X-Mailer: PHP v'.phpversion();
$x=#mail($email, $subject, $msg, $hdr);
if($x==0)
{
$email=str_replace('#','\#', $email);
$hdr=str_replace('#','\#',$hdr);
$x=#mail($email, $subject, $msg, $hdr);
}
return $x;
}
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" ... />