Example 1: how to send email with php
<?php
var Name = $_POST('name');
// grabbing the data from the form when posted
// make sure to add name="name" or name="email"
// in the input tag to grab the the specific elements
// data example <textarea name="message"> or
// <input type="number name="number">
var Email = $_POST('email');
var Number = $_POST('number');
$to_email = 'Your E-mail';
$subject = 'The Subject of the message';
$message = 'Name'.$name. "email" .$email. "number:" .$number.".";
$headers = 'From: noreply @ company . com'; //optional
mail($to_email,$subject,$message,$headers);
?>
Example 2: html php email
$mailtext = '<html>
<head>
<title>HTML-E-Mail mit PHP erstellen</title>
</head>
<body>
...
</body>
</html>
';
$empfaenger = "[email protected]"; // Mailadresse
$absender = "[email protected]";
$betreff = "Mail-Test - HTML-E-Mail mit PHP erstellen";
$antwortan = "[email protected]";
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset=utf-8\r\n";
$header .= "From: $absender\r\n";
$header .= "Reply-To: $antwortan\r\n";
// $header .= "Cc: $cc\r\n"; // falls an CC gesendet werden soll
$header .= "X-Mailer: PHP ". phpversion();
mail( $empfaenger,
$betreff,
$mailtext,
$header);
echo "Mail wurde gesendet!";
Example 3: read an email with php
//docs
https://www.php.net/manual/en/book.imap.php
https://garrettstjohn.com/articles/reading-email-php/
Example 4: read an email with php
<?php
//Modify it for your project
class Email_reader {
// imap server connection
public $conn;
// inbox storage and inbox message count
private $inbox;
private $msg_cnt;
// email login credentials
private $server = 'yourserver.com';
private $user = '[email protected]';
private $pass = 'yourpassword';
private $port = 143; // adjust according to server settings
// connect to the server and get the inbox emails
function __construct() {
$this->connect();
$this->inbox();
}
// close the server connection
function close() {
$this->inbox = array();
$this->msg_cnt = 0;
imap_close($this->conn);
}
// open the server connection
// the imap_open function parameters will need to be changed for the particular server
// these are laid out to connect to a Dreamhost IMAP server
function connect() {
$this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
}
// move the message to a new folder
function move($msg_index, $folder='INBOX.Processed') {
// move on server
imap_mail_move($this->conn, $msg_index, $folder);
imap_expunge($this->conn);
// re-read the inbox
$this->inbox();
}
// get a specific message (1 = first email, 2 = second email, etc.)
function get($msg_index=NULL) {
if (count($this->inbox) <= 0) {
return array();
}
elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
return $this->inbox[$msg_index];
}
return $this->inbox[0];
}
// read the inbox
function inbox() {
$this->msg_cnt = imap_num_msg($this->conn);
$in = array();
for($i = 1; $i <= $this->msg_cnt; $i++) {
$in[] = array(
'index' => $i,
'header' => imap_headerinfo($this->conn, $i),
'body' => imap_body($this->conn, $i),
'structure' => imap_fetchstructure($this->conn, $i)
);
}
$this->inbox = $in;
}
}
?>
A fair amount of this is
Example 5: send email in php
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "[email protected]";
$to = "[email protected]";
$subject = "Checking PHP mail";
$message = "PHP mail works just fine";
$headers = "From:" . $from;
if(mail($to,$subject,$message, $headers)) {
echo "The email message was sent.";
} else {
echo "The email message was not sent.";
}
Example 6: PHP Email Form
<form enctype="multipart/form-data" method="POST" action="">
<label>Your Name <input type="text" name="sender_name" /> </label>
<label>Your Email <input type="email" name="sender_email" /> </label>
<label>Subject <input type="text" name="subject" /> </label>
<label>Message <textarea name="message"></textarea> </label>
<label>Attachment <input type="file" name="attachment" /></label>
<label><input type="submit" name="button" value="Submit" /></label>
</form>