I'm having an issue with a contact form. The email message is correctly sent but in my inbox appears as "From: anonymous#web.godns.net" and not "From: web#mywebsite.com", and goes directly to SPAM.
I've been looking for similar issues but no one gives the specific answer. I guess if the code is having a syntax mistake or it's a server problem.
This is the PHP file:
<?php
// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
http_response_code(500);
exit();
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = "myemail#gmail.com";
$subject = "Mensaje Web de >> $name";
$body = "Recibiste mensaje a través del formulario en la web.\n"."Estos son los datos:\n\nNombre y Apellido: $name\n\nEmail: $email\n\nTeléfono: $phone\n\nTexto del mensaje:\n$message";
$headers= "From: web#mywebsite.com" . "\r\n" .
"Reply-To: $email \r\n" .
'X-Mailer: PHP/' . phpversion();
if(!mail($to, $subject, $body, $headers))
http_response_code(500);
?>
Does anyone figure out what's wrong here?
Thank you,
Alejandra | aleare.design
It may solve your problem or not but it's always a good idea to set a proper sender address. In good old mail() that needs to be done with the $additional_parameters parameter and the syntax is -f followed by an email address with no display name:
mail($to, $subject, $body, $headers, '-fweb#mywebsite.com')
Additionally, make sure that your local SMTP server allows sending messages in the name of web#mywebsite.com. If it doesn't, perhaps you need to get another server and make use of authentication, something that mail() doesn't allow.
In any case, it's really hard to get email right with this function since you need to do everything by yourself and email protocol is not trivial. For instance, I think your code is vulnerable to email headers injection. It's easier to just use a third-party library like Swift Mailer or PHPMailer.
P.S. What are you trying to accomplish with strip_tags(htmlspecialchars())? This will just make user input unreadable for not obvious gain.
This might help you, I don't get it why you put so many text in the header
// Create the email and send the message
$to = "youremail#yourdomain.com"; // Add your email address inbetween the " " replacing username#yourdomain.com - This is where the form will send a message to.
$subject = "Website Contact Form: $name";
$body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: noreply#adamar.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$header .= "Reply-To: $email"; // this is to automatically reply to your correspondent email rather than your noreply email, I separate it from your header.
also mind to change the variable $headers to $header? Maybe it might help. Wish all the best of it. Cheers!
Related
I've adapted a simple contact form using php which works fine. However I now need to add an auto response email, so when the client has entered details into the form it not only sends me the details, but sends the client a html email response.
Can any of you kind clever people point me in the right direction on how to achieve this as I'm stuck. Below is the code I'm using; am I right in thinking I need to use another "if" statement to send auto reply?
Any help will be greatly appreciated
<?php
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = 'andy#andydry.com';
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact
form.\n\n"."Here are the details:\n\nName: $name\n\nEmail:
$email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Send another email, but to the client, when the first email is sent successfully. That is assuming you're sending the client an email with different content than you're sending yourself when they use the contact form. Otherwise, use BCC.
Also, use a mail library such as Swiftmailer.
First off, a caveat: the php mail() function is prone to header-injection abuse, which can be used to force your mail server to spam emails on spammers behalf. Strictly filter incoming email addresses
As far as your question, you seem to be wanting to add a CC to your email of the customer's email address.
That is done through adding to the headers:
$headers .= "CC: ".$customer_email_address_sanitized_and_escaped."\r\n";
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps
I get this script for sending mails form form in my webpage, but lately when someone send message it goes to spam folder in gmail. My provider say it's because my from field is fake and get rated by google as spam. And that I should open email account on my domain and redirect form to that mail. I don't wont do that, so is there any workaround to send it to gmail?
$name = $_POST['name'];
$email = trim($_POST['email']);
$message= stripslashes($_POST['message']);
$subject = "poruka od $name";
$mail_send = " Ime: ".
$name.
"\nE-mail: ".
$email.
"\nPoruka: ".
$message;
$mail = mail(WEBMASTER_EMAIL, $subject, $mail_send,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
Can can do this by using PHPMailer class.
Just study about it.You have to provide your Email id and Password in that.
I have been looking at this for hours now, and I can't figure out why this won't work. I'm trying to send an email using the mail function. For some reason this page works when hosted by iPage, but not by Godaddy. What is the reason for this?
The PHP:
<?php
// Run code if button pressed
if (isset($_POST['submit'])) {
// Makes sure all fields are filled
if (!$_POST['name'] | !$_POST['email'] | !$_POST['message'] ) {
?><script>alert('You forgot to fill in a field');window.location = "http://example.com/contact.php";</script>
<?php
exit;
}
// From the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
$to = 'email#gmail.com';
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
?><script>alert('Thanks! I will try to get back to you as soon as possible.');window.location = "http://example.com/contact.php";</script><?php
}
?>
You need to look at your if condition you need || to do a OR
<?php
// Run code if button pressed
if (isset($_POST['submit'])) {
// Makes sure all fields are filled
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ) {
?>
<script>alert('You forgot to fill in a field');
window.location = "http://example.com/contact.php";
</script>
<?php
exit;
}
You are setting From email id $email from user Input, that means its not configured at your server, add a Reply-To header to $email
$headers = "From: youremail#yourdomain.com \r\n";
$headers .= 'Reply-To: '.$email. "\r\n" .
$headers .= "Content-type: text/html\r\n";
Final note, I would suggest you to use Swift Mailer or PHP Mailer to make it simple
For people having trouble sending email on GoDaddy's servers, here's whats going on:
To prevent spam, GoDaddy's servers refuse to send your mail if your using a certain domain (see below for the list). So to solve the specific problem here, I had to change the domain I was using, so $to = 'email#gmail'; is now $to = 'email#something-else.com';
Here is GoDaddy's explanation and the list of blacklisted domains:
Forms are popular on websites; they let customers share their information with you, whether it’s for a newsletter or to register an account. Often times, these forms email a confirmation of the visitor’s submission to to you or the visitor submitting the information. Did you know that all email, even if it’s sent from a hosting account, must have information entered in the From value, though?
You can make that From value a particular email address, too. This lets you create a professional-looking email for your customers or something that’s easy to categorize for yourself.
However, we have to be careful with what users can specify as their From value to combat spamming attempts. Here’s a list of email address domain names we don’t let our customers use as the From value of their forms:
gmail.com
aol.com
aim.com
yahoo.com
hotmail.com
live.com
msn.com
If your email form uses one of these domain names for its From email address, our server will not send the email.
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 :)