如何使用表单将邮件发送到任何电子邮件帐户(gmail,hotmail)?

时间:2015-05-13 11:24:37

标签: php email

我正在使用xampp v3.2.1,

我想使用表单向任何电子邮件帐户(gmail,hotmail)发送邮件。 我如何使用下面的代码来做到这一点?

<?php 
    mail($to,$subject,$body,"From:{$email}");
?>

当我使用上述邮件功能发送电子邮件时我找到了 我的电子邮件是&#34; C:\ xampp \ mailoutput&#34;;

目录中的文本文件

这是我的代码..

<?php

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

    //validate subject
    if(empty($_POST['subject'])){
    $errors[]="Enter a Subject";
    }else{
    $subject =htmlentities($_POST['subject']);
    }

    //validate sender
    if(empty($_POST['sender'])){
    $errors[]="Enter your email Address";
    }elseif(strlen($_POST['sender'])>200){
    $errors[] = "provided email address is too long";
    }elseif(filter_var($_POST['sender'],FILTER_VALIDATE_EMAIL)===false){
    $errors[]= "Provide a valid Email address!";
    }else{
    $email = "<".htmlentities($_POST['sender']).">";
    }

    #validate receiver
    if(empty($_POST['receiver'])){
    $errors[] = "Enter a email Address";
    }elseif(strlen($_POST['receiver'])>200){
    $errors[] = "provided reciever's email address is too long";
    }elseif(filter_var($_POST['receiver'],FILTER_VALIDATE_EMAIL)===false){
    $errors[]= "Provide a valid Email address to reciever!";
    }else{
    $to ="<".htmlentities($_POST['receiver']).">";
    }
    //validate message
    if(empty($_POST['message'])){
    $errors[] = "Enter a Message";
    }else{
    $body = htmlentities($_POST['message']);
    }


}
?>



<!Doctype html>
<html>
<head>
</head>
<body>
<?php if(empty($errors)===false){ ?>

<ul>
    <?php 
        foreach($errors as $error){
        echo "<li>",$error,"</li>";
        }
    ?>
</ul>
<?php }else{
    if(isset($to, $subject, $body, $email)){ 
    mail($to,$subject,$body,"From:{$email}");
    echo "Message Sent!";}

}
?>
<h1>SENDING EMAIL TEST</h1>
<form action="index.php" method="post">
<table>
<tr>
<td>Subject:</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td>Your eMail:</td>
<td><input type="email" name="sender"></td>
</tr>
<tr>
<td>Receiver's eMail:</td>
<td><input type="email" name="receiver"></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="message" cols="60" rows="20"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="SendMail"></td>
<td>&nbsp;</td>
</tr>

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

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

// apply all the rest validation you used
        $to=$_POST['receiver'];
        $subject=$_POST['subject'];
        $body=$_POST['message'];
        $from=$_POST['sender'];
        $mail=mail($to,$subject,$body,$from);
        if($mail!=false)
        {
            echo "Message Sent!";
        }
    }