-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailTo.php
More file actions
78 lines (69 loc) · 2.3 KB
/
mailTo.php
File metadata and controls
78 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html>
<head>
<title>Mail To...</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css">
</head>
<body>
<?php
include "makeHTML.php";
$msg = '';
$name = '';
$email = '';
$text = '';
if($_SERVER["REQUEST_METHOD"] === "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$text = $_POST["text"];
$alert = "";
if($name === "") {
$alert .= "Name field is required!<br>";
} elseif(!preg_match("/^[a-zA-Z'-]+$/",$name)) {
$alert .= "Invalid name.<br>";
}
if($email === "") {
$alert .= "Email field is required!<br>";
} elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$alert .= "Invalid email.<br>";
}
if($text === "") {
$alert .= "Message field is required!<br>";
}
if($alert !== "") {
$msg = makeHTML("div", "<strong>Something went wrong</strong><br>".$alert, ['class="alert alert-danger"', 'role="alert"']);
} else {
$emailTo = "timeline.mb@gmail.com";
$subject = "Your Website Email";
$body = "Name: ".$name."\n"."Message: ".$text;
$headers = "From: ".$_POST['email'];
if(mail($emailTo, $subject, $body, $headers)) {
$str = "Your email was correctly sent! We will contact you soon ".$name.".";
$msg = makeHTML("div", $str, ['class="alert alert-success"', 'role="alert"']);
} else {
$str = "Something went wrong, try to send later.";
$msg = makeHTML("div", $str, ['class="alert alert-danger"', 'role="alert"']);
}
}
}
?>
<div class="container my-4">
<?php echo $msg; ?>
<h1>Send A Email Via PHP</h1>
<form method="POST">
<fieldset class="form-group">
<label for="name">Name</label>
<input id="name" class="form-control" type="text" name="name" value="<?= $name ?>" placeholder="Name">
</fieldset>
<fieldset class="form-group">
<label for="email">Email</label>
<input id="email" class="form-control" type="text" name="email" value="<?= $email ?>" placeholder="Email">
</fieldset>
<fieldset class="form-group">
<label for="body">Message</label>
<textarea id="text" class="form-control" type="text" name="text" placeholder="Message" rows="5"><?= $text ?></textarea>
</fieldset>
<input class="btn btn-primary btn-lg" type="submit" value="Submit">
</form>
</div>
</body>
</html>