【问题标题】:send email to owner by collecting information provided by user through form?通过收集用户通过表单提供的信息向所有者发送电子邮件?
【发布时间】:2013-11-28 20:16:31
【问题描述】:

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

  1. 如何向所有者发送电子邮件?
  2. 如何添加所有信息,以便稍后在所有者电子邮件中取回或更改。

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)更改它,但它不起作用。

【问题讨论】:

  • 托管还是本地主机?您目前是否收到任何电子邮件?
  • @Dagon 首先我想检查本地主机然后我想在线使用它!我也可以从输入表单中正确获取所有信息,并希望将这些信息发送给他的 gmail 帐户中的网站所有者。
  • 当前显示的消息是 WARNING: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, 验证 php.ini 中的 "SMTP" 和 "smtp_port" 设置或在...中使用 ini_set()

标签: php email phpmailer


【解决方案1】:

有不同的方法:

1) 您可以使用PHP MAILER 库发送电子邮件。所以这个库完全基于 SMTP 。您必须指定您的第三方 SMTP 服务和凭据。您可以通过 gmail、yahoo 等发送电子邮件。通过使用这个库。该库将处理证券、标头等,并且非常易于使用。

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

我建议你使用 PHP Mailer 集成你的 SMTP 服务并启动 Mailing。

【讨论】:

    【解决方案2】:

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

    联系人.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>
    

    【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 2014-08-17
      • 1970-01-01
      • 2014-11-27
      • 2015-09-10
      • 2017-01-10
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多