首页 文章

通过收集用户通过表格提供的信息向所有者发送电子邮件?

提问于
浏览
1

我设计了用户输入表单,用于收集用户名,电子邮件地址,主题和消息 . 我已经验证了所有元素都存储在变量中 . 现在我需要通过电子邮件将此信息发送给所有者 . 我怎样才能在localhost中解决这个问题呢?

  • 如何向所有者发送电子邮件?

  • 如何添加所有信息,以便我可以在以后的所有者电子邮件中找回或区别 .

contact.php

<form action="contact_ok.php" method="post"  autocomplete="on">
<p>
    <label for="username"> Name <span class="required">*</span></label>
    <input type="text" name="username" required="TRUE"/>
</p>
<p> 
    <label for="usermail"> E-mail address <span class="required">*</span></label>
    <input type="email" name="usermail"required="TRUE"/>
</p>
<p>
    <label for="subject"> Subject </label>
    <input type="text" name="subject" required="TRUE" />
</p>
<p>
    <label for="message"> Message  <span class="required">*</span></label>
    <textarea name="msg" required="TRUE" ></textarea>
</p> 
<input type="submit" name="submit mail"/>   
</form>

contact_ok.php

<?php
$name = addslashes($_POST['username']);
$uml = addslashes($_POST['usermail']);
$sub = addslashes($_POST['subject']);
$msg =addslashes($_POST['msg']);
//sending mail to owner
$to ='owner@gmail.com';
//enter input information in array ******
//send email
mail($to,$sub,$msg);
?>

为了发送邮件,我读了一下PHP邮件,但我发现很难理解 . 我也研究了this thread on SO这还不够 . 2.为了存储我试图通过使用 $msg=str_split($msg) 首先将消息转换为数组的所有元素,并使用 push_array($msg) 推送所有其他信息并使用 print_r($msg,TRUE) 更改它但它不起作用 .

3 回答

  • 0

    如果您想使用Gmail地址发送自动邮件,那么我建议您使用以下链接:https://www.google.co.za/search?q=using+swiftmailer+and+sending+with+gmailhttp://www.swiftmailer.org/docs/sending.html

  • 0

    有不同的方式:

    1)您可以使用PHP MAILER库发送电子邮件 . 所以这个库完全基于SMTP . 您必须指定第三方SMTP服务和凭据 . 您可以通过Gmail,雅虎等发送电子邮件 . 通过使用此库 . 该库将关注证券, Headers 等,并且非常易于使用 .

    2)我不推荐的另一种方法是安装自己的postfix . 配置所有内容并发送电子邮件 . 它非常耗时,你必须添加各种级别的身份验证,如SPF,DMARC等 .

    我想建议你使用PHP Mailer集成你的SMTP服务并开始邮件 .

  • 0

    首先我们让mailto函数工作 . (使用localhost和电子邮件程序,如Thunderbird或Outlook或Mail)
    此后,您可以使用Swiftmailer . (比PHPmailer更容易)我添加了一些javascript验证P.S.最好使用textareas而不是文本框,因为它们保留了空格 . 稍后当您添加数据库代码时,您可以使用TEXT而不是VARCHAR . (我遗漏了一些你需要填写的东西)享受!

    contact.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <title>Registration</title>  
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  
    <script type="text/javascript">  
    
    function formValidator(){  
    var subject = document.getElementById('subject');  
    var usermail = document.getElementById('useremail');  
            if(notEmpty(subject, "Please enter an email subject")){  
            if(emailValidator(usermail, "Please enter email addr")){  
                            return true;  
        }  
    }  
    return false;
    
    }//very important end of formvalidator!!
    
    function notEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }
        return true;
    }
    function emailValidator(elem, helperMsg){
        var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        if(elem.value.match(emailExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
    </script>
    </head>
    <body>
    <form onsubmit="return formValidator()" action="contact_ok.php" method="post">
    
    <textarea id='username' name='username'  style='white-space:pre-wrap; height:18px;font-family:arial;width:200px;font-size: 10pt'  >
    </textarea>  
    </body>  
    </html>
    

    contact_ok.php

    <?php
    $username = "_";
    $usermail = "_";
    $subject  = "_";
    $msg = = "_";
    $from ='owner@gmail.com';
    
    $username = ($_POST['username']);
    $usermail = ($_POST['usermail']);
    $subject = ($_POST['subject']);
    $msg =($_POST['msg']);
    
    $aa = "Hi";
    $a0 = "Welcome.";
    $nl = "%0D%0A"; //new line
    echo "<br><br>";
    ?>
    
    <font size = 4><b>
    <a href="mailto:<?php echo $useremail?>?subject=<?php echo $subject; ?>&body=<?php echo $aa.$nl.$a0.$nl ?>">
    Click to EMail  customer</a><br>
    </font>
    <br>
    

相关问题