Jump to content

Recommended Posts

Hi, New to php here please help me out.

So I have been trying to use the mail function to send mail to me, but it's not working I know i changed the fakeemail@example.com to mine, but it's not working and I can't figure out why. Plus do you know how I can add the emailers name and his email sent additionally with the message to me. Thanks.

<?php  

if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
	if (mail('fakeemail@example.com','New Website Message', $_POST['message'])) {
		$status = "Thank you for your message {$_POST['email']}";
	} 
}

?>
<html>
<head>
	<title></title>
	<style>
		label {display: block;}
		form ul {margin: 0; padding: 0;}
		form li {list-style: none; margin-bottom: 20px;}
	</style>
</head>
<body>
	<h1>Contact Form</h1>
	<form action="" method="post">
		<ul>
			<li>
				<label for="name">Name: </label>
				<input type="text" name="name" id="name">
			</li>

			<li>
				<label for="email">Email: </label>
				<input type="text" name="email" id="email">
			</li>
	
			<li>
				<label for="message">Your Message: </label><br />
				<textarea name="message" id="message" cols="30" rows="10"></textarea>
			</li>

			<li>
				<input type="submit" value="Go!">
			</li>
		</ul>
	</form>
	<?php  if (isset($status)) echo $status; ?>
</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/275944-mail-function-php/
Share on other sites

 

Hi, New to php here please help me out.

So I have been trying to use the mail function to send mail to me, but it's not working I know i changed the fakeemail@example.com to mine, but it's not working and I can't figure out why. Plus do you know how I can add the emailers name and his email sent additionally with the message to me. Thanks.

<?php  

if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
	if (mail('fakeemail@example.com','New Website Message', $_POST['message'])) {
		$status = "Thank you for your message {$_POST['email']}";
	} 
}

?>
<html>
<head>
	<title></title>
	<style>
		label {display: block;}
		form ul {margin: 0; padding: 0;}
		form li {list-style: none; margin-bottom: 20px;}
	</style>
</head>
<body>
	<h1>Contact Form</h1>
	<form action="" method="post">
		<ul>
			<li>
				<label for="name">Name: </label>
				<input type="text" name="name" id="name">
			</li>

			<li>
				<label for="email">Email: </label>
				<input type="text" name="email" id="email">
			</li>
	
			<li>
				<label for="message">Your Message: </label><br />
				<textarea name="message" id="message" cols="30" rows="10"></textarea>
			</li>

			<li>
				<input type="submit" value="Go!">
			</li>
		</ul>
	</form>
	<?php  if (isset($status)) echo $status; ?>
</body>
</html>

 

Your logic should look something like this:

 

if(isset($_POST['email'])) 
{

$to  = 'YourEmail@host.com';

$subject = 'Your Subject Line goes here';
$message = $_POST['message'];

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


// Mail it
mail($to, $subject, $message, $headers);



}

 

 

You can add in additional headers etc. which is all explained here - http://php.net/manual/en/function.mail.php

 

But that should get you started. You probably want a better way of checking if the form has been submitted - i just used $_POST email for an example.

Link to comment
https://forums.phpfreaks.com/topic/275944-mail-function-php/#findComment-1419960
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.