PHP 'email sent' but not received - php

Stuck on a simple php mail thing. Can anybody spot where I've gone wrong. Would be happy for any help.
<?php
$to = "example#website.co.uk";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
mail($name, $email, $subject, $message);
if(mail($name, $email, $subject, $message)) {
echo "E-Mail Sent";
} else {
echo "There was a problem";
}
?>

The first comment pretty much said it all, if you need a beginner-friendly tutorial of the mail() function, you should check out PHP Sending E-mails
<?php
$to = "example#website.co.uk";
$name = 'From:'.$_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if(mail($to, $subject, $message, $name)) {
echo "E-Mail Sent";
} else {
echo "There was a problem";
}
?>

Try this instead, it would need spam proofing etc...
<?php
$to = 'example#example.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $message, $headers);
?>

Related

How do I make my contact page working in PHP?

I'm trying to make a simple contact page, but everytime I try to send a message, I get an error.
Here is the PHP code:
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "mailto:benten#benniestudios.eu";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $email, $formcontent, $mailheader) or die("Error!");
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
echo 'Return Home';
?>
What did I do wrong? If you need more info, please tell me.

Processing my form with required fields php

I'm just starting to learn php, so I suspect this is going to be an easy one. I want to create a form that has one required field (name) and requires at least one of two other fields (email or phone number). This is what I came up with:
<?php
if (isset($_POST["submit"])) {
if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {
$error = "Your name and a contact method are required fields";}
else {
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$contactmethod = $_POST["contactmethod"];
$subject = $_POST["subject"];
$location = $_POST["location"];
$message = $_POST["message"];
$subjectline = "You have a message from $name!";
$header = "From: $email";
$body = <<<EMAIL
Name: $name,
Number: $number,
Email: $email,
Subject: $subject,
Location: $location,
Contact method: $contactmethod.
$message
EMAIL;
mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
}
?>
I have no clue what's wrong.
Thanks
Your problem: You used { and } in a if-statement.
Next time you can use a PHP code checker like http://phpcodechecker.com/
Correct code:
if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {
$error = "Your name and a contact method are required fields";}
else {
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$contactmethod = $_POST["contactmethod"];
$subject = $_POST["subject"];
$location = $_POST["location"];
$message = $_POST["message"];
$subjectline = "You have a message from $name!";
$header = "From: $email";
$body = <<<EMAIL
Name: $name,
Number: $number,
Email: $email,
Subject: $subject,
Location: $location,
Contact method: $contactmethod.
$message
EMAIL;
mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
}
?>
You can't use braces interchangeably with parentheses.
if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {
it needs to use parentheses
if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {

PHP contact form with Freehostia

I cannot get this form to send if I use anything other than $from = 'From: . $email';. If I change it to anything else, it will not send. When it does send with this information, it comes in from .$email#mbox.freehostia.com.
What I would prefer is have the from email address be the email that was submitted in the form, so the receiver can respond without having to create a new email. I've searched everything and can't find an answer to this specific issue.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: . $email';
$to = 'info#resourcedmichigan.com';
$subject = 'ResourcED Career Submission';
$body = "From: $name\nEmail: $email\nPhone Number: $phone\nMessage: $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
include("inc/header.php");
echo '<div class="container"><div class="spacer-top"><h3>Thank you for your interest in ResourcED! We will be in contact with you soon!</h3></div></div>';
include("inc/footer.php");
} else {
echo '<div class="container"><h3>Something went wrong. Go back and try again!</h3></div>';
}
}
?>
Variables will not be interpolated inside of single quotes and the concatenation operator is unnecessary.
$from = 'From: . $email';
should be
$from = "From: $email";
or
$from = 'From: ' . $email;

adding some text into message in mail

$contactname = $_POST['contactname'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = 'Ձեզ գրել են ձեր կայքից';
$to = 'stereoshoots#gmail.com';
$headers = "From: ".$email;
mail($to,$sub,$message,$headers);
I got $contactname (client name). My task is the text like this :
From : $contactname
$message (the text that client wrote).
How should i implant name into text?
You can costumise, the below, but from contact name is in this case, $from
<?php
$to = "someone#example.com";
$subject = "Ձեզ գրել են ձեր կայքից";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers)){
echo "Mail Sent.";}
else{
echo "Mail not sent";
}
?>

email address in from field on contact form not showing in email header as "from"

iv added the header field in the form code but when i receive the email, the "from" field in the email shows a garbled code and not the email address of the person who sent the message. This is my message processing code:
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty($_SESSION['chapcha_code'] ) ) {
$youremail = 'I removed my address for privacy on stack overflow';
$fromsubject = 'A message from your website';
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $nome <$mail>\r\n";
$to = $youremail;
$mailsubject = 'Message received from'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is '.$fname.' '.$lname.'
Address: '.$address.'
'.$city.', '.$zip.', '.$country.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your feedback. I will contact you shortly if needed.<br/>Go to <a href='/index.php'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You declared your ^From: header` here:
$headers = "From: $nome <$mail>\r\n";
But it isn't used when you call mail()
mail($to, $subject, $body);
You need to pass $headers as fourth parameter. Else the headers won't take effect
mail($to, $subject, $body, $headers);
You haven't declared $nome anywhere in your code nor $mail in:
$headers = "From: $nome <$mail>\r\n";
And besides that, as this comment mentions, you didn't pass the header into the mail function. See the fourth parameter of the mail function.

Categories