send email to owner by collecting information provided by user through form?

只愿长相守 提交于 2020-01-05 04:57:13

问题


I had designed user input form that collects user name, email address, subject and message. I had verified all elements stored these element in variable. Now I need to send this information to the owner via email. How can I solve this problem in localhost(too)?

  1. How can I send email to owner?
  2. How can I add all information so that I can get back or differ it later in owner email.

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

For sending mail I read about PHP mailer but I found hard to understand. Also I studied this thread on SO it is also not sufficient. 2.For storing all elements I tried to convert message firstly to array by using $msg=str_split($msg) and push all other information by using push_array($msg) and change it by using print_r($msg,TRUE) but it does not work.


回答1:


There is different ways:

1) You can use PHP MAILER library to send Email. So this library is totally based on SMTP . You have to specify your third party SMTP services and credentials. You can send email through gmail, yahoo etc . by using this library. This library will take care about securities, headers etc and very easy to use.

2) Another way which is not recommend by me is install your own postfix. Configure all things and send an email. Its very time consuming and you have to add various level of authentication like SPF, DMARC etc.

I would like to recommend you to go with PHP Mailer integrate yous SMTP service and start Mailing.




回答2:


First we got the mailto function to work.(using localhost and an email program such as Thunderbird or Outlook or Mail)
Thereafter you can use Swiftmailer. (Easier than PHPmailer) I have added some javascript validation P.S. It's better to use textareas instead of textboxes becasue they keep whitespaces. later when you add a database code you can use TEXT instead of VARCHAR. (I left out a few things that you need to fill in) Enjoy!

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>



回答3:


If you want to use a gmail address to send automatic mails, then I recommend these links: https://www.google.co.za/search?q=using+swiftmailer+and+sending+with+gmail and http://www.swiftmailer.org/docs/sending.html



来源:https://stackoverflow.com/questions/20274158/send-email-to-owner-by-collecting-information-provided-by-user-through-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!