- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
In this post, I am assuming that you already understood about HTML and basic PHP including POST Method.
First of all, you have to create an HTML form with this code below.
form.html
<form method="post" target="send.php" class="form-horizontal">
<fieldset>
<legend>Email</legend>
<div class="form-group">
<label class="col-lg-2 control-label">To</label>
<div class="col-lg-4">
<input class="form-control" name="to" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Subject</label>
<div class="col-lg-1">
<input class="form-control" name="ubject" type="text">
</div>
</div>
<div class="form-group">
<label for="textArea" class="col-lg-2 control-label">Message</label>
<div class="col-lg-8">
<textarea name="msg" class="form-control" rows="3" id="msg"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Attachment</label>
<div class="col-lg-5">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<input type="file" class="form-control" name="file[]" id="file" multiple="yes" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</fieldset>
</form>
java.js
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="file" class="form-control" name="file[]" id="file" multiple="yes" /></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
send.php
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "yourmail@gmail.com";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "yourmail@gmail.com";
$password = "yourpassword";
$to = $_POST['to'];
$subject = $_POST['subject'];
$body = $_POST['msg'];
$crlf = "\n";
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'multipart/mixed; boundary=frontier';
$headers['Content-Transfer-Encoding'] = '7bit';
$headers['Date'] = date('r', $_SERVER['REQUEST_TIME']);
$headers['Message-ID'] = $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'];
$headers['From'] = "do-not-reply@sbm-itb.ac.id";
$headers['To'] = $to;
$headers["Reply-To"] = "guruh.sapta@sbm-itb.ac.id";
$headers['Subject'] = $subject;
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($body);
foreach($_FILES['file']['tmp_name'] as $key => $value){
$file_name = $_FILES['file']['name'][$key];
$tmp_name = $_FILES['file']['tmp_name'][$key];
$file_type = $_FILES['file']['type'][$key];
move_uploaded_file($tmp_name, "attachment/".$file_name);
$mime->addAttachment("attachment/".$file_name, $file_type);
}
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array('host' => $host,
'port' => $port, 'auth' => true,
'username' => $username, 'password' => $password));
$mail = $smtp -> send($to, $headers, $body);
When you clicked the Send button, let magic happen.
:)
- Get link
- X
- Other Apps
Comments
Post a Comment
Please leave your comment politely and do not write a spam message.
Thank you. :)