我设计了用户输入表单,用于收集用户名,电子邮件地址,主题和消息 . 我已经验证了所有元素都存储在变量中 . 现在我需要通过电子邮件将此信息发送给所有者 . 我怎样才能在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 回答
如果您想使用Gmail地址发送自动邮件,那么我建议您使用以下链接:https://www.google.co.za/search?q=using+swiftmailer+and+sending+with+gmail和http://www.swiftmailer.org/docs/sending.html
有不同的方式:
1)您可以使用PHP MAILER库发送电子邮件 . 所以这个库完全基于SMTP . 您必须指定第三方SMTP服务和凭据 . 您可以通过Gmail,雅虎等发送电子邮件 . 通过使用此库 . 该库将关注证券, Headers 等,并且非常易于使用 .
2)我不推荐的另一种方法是安装自己的postfix . 配置所有内容并发送电子邮件 . 它非常耗时,你必须添加各种级别的身份验证,如SPF,DMARC等 .
我想建议你使用PHP Mailer集成你的SMTP服务并开始邮件 .
首先我们让mailto函数工作 . (使用localhost和电子邮件程序,如Thunderbird或Outlook或Mail)
此后,您可以使用Swiftmailer . (比PHPmailer更容易)我添加了一些javascript验证P.S.最好使用textareas而不是文本框,因为它们保留了空格 . 稍后当您添加数据库代码时,您可以使用TEXT而不是VARCHAR . (我遗漏了一些你需要填写的东西)享受!
contact.php
contact_ok.php