1<?php
2if(isset($_POST['submit'])){
3 $to = "email@example.com"; // this is your Email address
4 $from = $_POST['email']; // this is the sender's Email address
5 $first_name = $_POST['first_name'];
6 $last_name = $_POST['last_name'];
7 $subject = "Form submission";
8 $subject2 = "Copy of your form submission";
9 $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
10 $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
11
12 $headers = "From:" . $from;
13 $headers2 = "From:" . $to;
14 mail($to,$subject,$message,$headers);
15 mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
16 echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
17 // You can also use header('Location: thank_you.php'); to redirect to another page.
18 }
19?>
20
21<!DOCTYPE html>
22<head>
23<title>Form submission</title>
24</head>
25<body>
26
27<form action="" method="post">
28First Name: <input type="text" name="first_name"><br>
29Last Name: <input type="text" name="last_name"><br>
30Email: <input type="text" name="email"><br>
31Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
32<input type="submit" name="submit" value="Submit">
33</form>
34
35</body>
36</html>
1// use this library -> https://github.com/PHPMailer/PHPMailer
2
3<?php
4use PHPMailer\PHPMailer\PHPMailer;
5use PHPMailer\PHPMailer\SMTP;
6use PHPMailer\PHPMailer\Exception;
7function sendEmail(){
8 require 'phpmailer/vendor/autoload.php';
9
10 //Create an instance; passing `true` enables exceptions
11 $mail = new PHPMailer(true);
12 $mail->CharSet = 'UTF-8';
13 try {
14 $receiver = 'test@gmail.com';
15 $name = 'Name';
16
17 //Server settings
18 $mail->SMTPDebug = 1; //Enable verbose debug output
19 $mail->isSMTP(); //Send using SMTP
20 $mail->Host = 'tls://smtp.gmail.com'; //Set the SMTP server to send through
21 $mail->SMTPAuth = true; //Enable SMTP authentication
22 $mail->Username = 'username@gmail.com'; //SMTP username
23 $mail->Password = 'PASSWORD'; //SMTP password
24 $mail->SMTPSecure = tls; //Enable implicit TLS encryption
25 $mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
26
27 //Recipients
28 $mail->From = 'test@gmail.com';
29 $mail->setFrom('test@gmail.com', 'Name');
30 $mail->addAddress($receiver, $name); //Add a recipient
31
32 //Content
33 $mail->isHTML(true); //Set email format to HTML
34 $mail->Subject = 'Subject';
35 $mail->Body = 'Body';
36
37 $mail->send();
38 echo 'Message has been sent';
39 } catch (Exception $e) {
40 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
41 }
42}
43?>
1<?php
2//Modify it for your project
3class Email_reader {
4
5 // imap server connection
6 public $conn;
7
8 // inbox storage and inbox message count
9 private $inbox;
10 private $msg_cnt;
11
12 // email login credentials
13 private $server = 'yourserver.com';
14 private $user = 'email@yourserver.com';
15 private $pass = 'yourpassword';
16 private $port = 143; // adjust according to server settings
17
18 // connect to the server and get the inbox emails
19 function __construct() {
20 $this->connect();
21 $this->inbox();
22 }
23
24 // close the server connection
25 function close() {
26 $this->inbox = array();
27 $this->msg_cnt = 0;
28
29 imap_close($this->conn);
30 }
31
32 // open the server connection
33 // the imap_open function parameters will need to be changed for the particular server
34 // these are laid out to connect to a Dreamhost IMAP server
35 function connect() {
36 $this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
37 }
38
39 // move the message to a new folder
40 function move($msg_index, $folder='INBOX.Processed') {
41 // move on server
42 imap_mail_move($this->conn, $msg_index, $folder);
43 imap_expunge($this->conn);
44
45 // re-read the inbox
46 $this->inbox();
47 }
48
49 // get a specific message (1 = first email, 2 = second email, etc.)
50 function get($msg_index=NULL) {
51 if (count($this->inbox) <= 0) {
52 return array();
53 }
54 elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
55 return $this->inbox[$msg_index];
56 }
57
58 return $this->inbox[0];
59 }
60
61 // read the inbox
62 function inbox() {
63 $this->msg_cnt = imap_num_msg($this->conn);
64
65 $in = array();
66 for($i = 1; $i <= $this->msg_cnt; $i++) {
67 $in[] = array(
68 'index' => $i,
69 'header' => imap_headerinfo($this->conn, $i),
70 'body' => imap_body($this->conn, $i),
71 'structure' => imap_fetchstructure($this->conn, $i)
72 );
73 }
74
75 $this->inbox = $in;
76 }
77
78}
79
80?>
81A fair amount of this is
1<?php
2 ini_set( 'display_errors', 1 );
3 error_reporting( E_ALL );
4 $from = "test@hostinger-tutorials.com";
5 $to = "test@hostinger.com";
6 $subject = "Checking PHP mail";
7 $message = "PHP mail works just fine";
8 $headers = "From:" . $from;
9 if(mail($to,$subject,$message, $headers)) {
10 echo "The email message was sent.";
11 } else {
12 echo "The email message was not sent.";
13 }
1$mailtext = '<html>
2<head>
3 <title>HTML-E-Mail mit PHP erstellen</title>
4</head>
5
6<body>
7...
8</body>
9</html>
10';
11
12$empfaenger = "du@example.com"; // Mailadresse
13$absender = "ich@example.com";
14$betreff = "Mail-Test - HTML-E-Mail mit PHP erstellen";
15$antwortan = "ICH@example.com";
16
17$header = "MIME-Version: 1.0\r\n";
18$header .= "Content-type: text/html; charset=utf-8\r\n";
19
20$header .= "From: $absender\r\n";
21$header .= "Reply-To: $antwortan\r\n";
22// $header .= "Cc: $cc\r\n"; // falls an CC gesendet werden soll
23$header .= "X-Mailer: PHP ". phpversion();
24
25mail( $empfaenger,
26 $betreff,
27 $mailtext,
28 $header);
29
30echo "Mail wurde gesendet!";
1<form enctype="multipart/form-data" method="POST" action="">
2 <label>Your Name <input type="text" name="sender_name" /> </label>
3 <label>Your Email <input type="email" name="sender_email" /> </label>
4 <label>Subject <input type="text" name="subject" /> </label>
5 <label>Message <textarea name="message"></textarea> </label>
6 <label>Attachment <input type="file" name="attachment" /></label>
7 <label><input type="submit" name="button" value="Submit" /></label>
8</form>
9