我如何使用PHP发送电子邮件?

时间:2014-12-02 08:54:21

标签: php html5 phpmyadmin xampp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>PHP form to email sample form</title>
<!-- define some style elements-->
<style>
label,a 
{
    font-family : Arial, Helvetica, sans-serif;
    font-size : 12px; 
}

</style>    
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>

<body>

<!-- Start code for the form-->
<form method="post" name="myemailform" action="wonder.php">
    <p>
        <label for='name'>Enter Name: </label><br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Enter Email Address:</label><br>
        <input type="text" name="email">
    </p>
    <p>
        <label for='message'>Enter Message:</label> <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
</form>
<script language="JavaScript">

// Code for validating the form
    // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
    // for details
    var frmvalidator  = new Validator("myemailform");
    frmvalidator.addValidation("name","req","Please provide your name"); 
    frmvalidator.addValidation("email","req","Please provide your email"); 
    frmvalidator.addValidation("email","email","Please enter a valid email address"); 
    </script>
    <p>
    <!-- <a href='wonder.php' -->
    >PHP form to email article page</a>
</p>
</body>
</html>

PHP表单:

<?PHP
$email = $_POST["emailaddress"];
$to = "chris.pagemotion@gmail.com.my";
$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: chris.pagemotion@gmail.com.my\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>

我是否需要像xampp或phpMyAdmin这样的本地服务器来完成这项工作?或者我做错了什么?它在我继续提交后不断给我PHP页面文本,我没有设置本地主机。

1 个答案:

答案 0 :(得分:0)

您需要一种运行PHP解释器的Web服务器。我建议使用开箱即用的解决方案,如XAMPP或WAMP。