Reputation: 871
.hi guys can anyone guide me on how to create a PHP form that sends an email to a desired recipient using the email provided by my domain server.
<?php
if($_POST['submit'])
{
$name = $_POST['name'];
$message = $_POST['message'];
if($name&&$message)
{
$namelen = 20;
$messagelen = 300;
if(strlen($name)<=$namelen&&strlen($message)<=$messagelen)
{
$to = "[email protected]";
$subject = "Test Email";
$headers = "From: my server provided email here";
ini_set("SMTP", "/*i placed my domain server here");
$body = "This is an email from $name\n\n$message";
mail($to, $subject, $body, $headers);
die();
}
else
die("Max length for name is $namelen, and max length for message is $messagelen.");
}
else
die("You must enter a name <u>and</u> message");
/*echo $name.' '.$message;*/
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action='mailpush.php' method='POST'>
Name:<input type='text' name='name' maxlength='20'><br>
Message:<br /><textarea name='message'></textarea><p>
<input type='submit' name='submit' value='Send me this'></p>
</form>
</body>
</html>
.this is the code i have so far. but when i try to send i don't receive anything.
Upvotes: 0
Views: 1229
Reputation: 48357
when i try to send i don't receive anything
So really your problem is not in writing the code, but in diagnosing why it's failing.
First thing of note is that you don't check the return value of the mail() call.
ini_set("SMTP", "/*i placed my domain server here");
What is this supposed to mean? There is no such thing as a "domain server". There are domain name servers, SMB domain masters, SMTP servers.....
Next, you've provided no details of the OS this is running on nor the config for mail in php.ini : although you're explicitly setting the SMTP host (to what?, is it resolvable?) what is the setting for smtp_port? Can you telnet to that port on the named machine from where the PHP code is running?
There are an awful lot of technology things between your code and your mailbox - many of which could cause problems with the mail delivery - have you looked at these? Your local SMTP server is just the next hop in the chain, did you check if your email was enqueued there? If it was, then its nothing to do with PHP.
Upvotes: 1