【问题标题】:how to Send mails to any email account(gmail,hotmail) using a form? [duplicate]如何使用表单将邮件发送到任何电子邮件帐户(gmail、hotmail)? [复制]
【发布时间】:2015-07-24 15:46:29
【问题描述】:

我正在使用 xampp v3.2.1,

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

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

当我使用上述邮件功能发送电子邮件时我发现 我的电子邮件作为“C:\xampp\mailoutput”目录中的文本文件;

这是我的代码..

<?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>

【问题讨论】:

    标签: php email


    【解决方案1】:
        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!";
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-02-02
      • 2016-02-22
      • 2012-03-30
      • 2014-11-30
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      相关资源
      最近更新 更多