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" ... />
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 10 months ago.
Hi guys I run my website and it all work except contact us form.
HTML
<div class="border"></div>
<form class="contact-form" action="mail.php" method="post">
<div class="fisrtthree">
<input type="text" name="fname" class="contact-form-text" placeholder="First name" required>
</div>
<div class="fisrtthree">
<input type="text" name="lname" class="contact-form-text" placeholder="Last name" required>
</div>
<div class="fisrtthree">
<input type="email" name="email" class="contact-form-text" placeholder="Email" required>
</div>
<div>
<textarea class="contact-form-text" name="message" rows="6" maxlength="3000" placeholder="Your message" required></textarea>
</div>
<div>
<button type="submit" id="fcf-button" class="contact-form-btn">Send</button>
<div>
</form>
PHP
<?php
if(isset($_POST['message']) == false) { // If there's no message
echo "Uh oh. Looks like you didn't actually include a message, friend.<br><br>";
die();
}
$destination = "##gmail.com"; // Put your email address here
$subject = "Message from your website!"; // Fill in the subject line you want your messages to have
$fromAddress = "##domain.com"; // Fill in the email address that you want the messages to appear to be from
// Use a real address to reduce the odds of getting spam-filtered.
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = str_replace("\n.", "\n..", $_POST['message']); // Prevents a new line starting with a period from being omitted
$message = "First Name: ". $fname ."\n Last Name: ". $lname ."\n Email: ". $email ."\n Message: ".$message."\n";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();
mail($destination, $subject, $message, implode("\r\n", $headers));
Thanks for your message:
echo $message; ?>
Go back home
?>
As you see I tried to get message from ##domain.com and received it in mygmail#gmail.com. but it didn't work, I received nothing in my inbox. is there anything to do with my gmail and my hosted email.
Your email is not being sent. You can try to capture the error that happened via php's last error function, after sending the email :
print_r(error_get_last());
Firstly, ensure that your email service provider makes allowance for the use of less secure apps, and make sure you have this feature enabled (Be sure to disable it later). My 'from' email was a Gmail account where non-secure apps can be enabled at this address: https://myaccount.google.com/lesssecureapps?pli=1
Secondly, ensure that your local mail server is correctly set up. If you are using XAMPP follow the directions here: https://www.geeksforgeeks.org/how-to-configure-xampp-to-send-mail-from-localhost-using-php/
Next, name the button on your form in order to detect the form submission, I named the button 'submit'.
Then in mail.php use this code
<?PHP
if(isset($_POST['submit']) && empty($_POST['message'])) {
// If there's no message
echo "Uh oh. Looks like you didn't actually include a
message, friend.<br><br>";
die();
}
$destination = "fihriabdelali#gmail.com";
$subject = "Message from your alfidomain.com!";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$fromAddress=$email;
$message = str_replace("\n.", "\n..", $_POST['message']);
// Prevents a new line starting with a period from being omitted
$message = "First Name: ". $fname ."\n Last Name: ".$lname ."\n Email: ". $email ."\n Message: ".$message."\n";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();
mail($destination, $subject, $message, implode("\r\n",
$headers));
// mail($to,$subject,$msg,$headers);
echo "Email successfully sent.";
?>
If you are not Abdelali make sure you set $destination to "youremail#domain.com"
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
I'm new to this site so i'll try and explain myself as clearly as possible!
I'm currently in the process of creating a web form which when submitted sends an email to an account I have made on my server.
All is working fine but was wondering if there was a way i could add multiple accounts for the submitted form to send too.
My HTML:
<table style="width:100%">
<form action="form_complete.php" method="post">
<tr>
<th>
<input type="text" required placeholder="Name" name="name" />
</th>
<th>
<input type="text" required placeholder="Email" name="email" />
</th>
</tr>
<tr>
<th>
<input type="text" placeholder="Contact Number" name="mobile" />
</th>
<th>
<input type="text" required placeholder="Subject" name="subject" />
</th>
</tr>
<tr>
<td colspan="2"><textarea id="ta" name="message" required placeholder="Message"></textarea>
</tr>
</table>
<input id="submit" type="submit" name="submit" value="submit" />
</form>
form_complete.php
if(isset($_POST['submit'])){
$to = "rtozer#tandtcivils.co.uk"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$subject2 = "Copy of your form submission";
$message = "Name: " .$name . "\n\n mobile number: " . $mobile . ".\n\n Message:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header ('Location: http://www.tandtcivils.co.uk/form_complete.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
The code here is working correctly when sending an email to rtozer#tandtcivils.co.uk but i want to be able to add; dtozer#tandtcivils.co.uk to recieve a copy of the form submission also.
If you need any further information from me please leave a comment!
Thanks in advance,
Sam
You can have multiple TO addresses. For example:
$to = "rtozer#tandtcivils.co.uk," . $_POST['email'];
Or use CC or BCC headers:
$headers = "From:" . $from;
$headers .= "CC:" . $_POST['email'];
Or:
$headers .= "CC:rtozer#tandtcivils.co.uk," . $_POST['email'];
There are a number of ways to organize your email recipients, depending on whether you want them to be direct recipients, carbon copy recipients, or blind carbon copy recipients. Lots of examples are available in the PHP documentation.
The key benefit here, as opposed to what you attempted in your code, is that you need only send the email once as opposed to sending the same email multiple times. Just arrange multiple recipients on that one email.
Thanks to Reza Mamun try this:
//......
//...Other setting goes here....
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: My Name <myemail#example.com>'. "\r\n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1#example.com, myboss2#example.com' . "\r\n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3#example.com, myboss4#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
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.