Receive email in PHP code example
Example 1: php mail function from name
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Jack Sparrow <[email protected]>' . PHP_EOL .
'Reply-To: Jack Sparrow <[email protected]>' . PHP_EOL .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Example 2: read an email with php
https:
https:
Example 3: read an email with php
<?php
class Email_reader {
public $conn;
private $inbox;
private $msg_cnt;
private $server = 'yourserver.com';
private $user = '[email protected]';
private $pass = 'yourpassword';
private $port = 143;
function __construct() {
$this->connect();
$this->inbox();
}
function close() {
$this->inbox = array();
$this->msg_cnt = 0;
imap_close($this->conn);
}
function connect() {
$this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
}
function move($msg_index, $folder='INBOX.Processed') {
imap_mail_move($this->conn, $msg_index, $folder);
imap_expunge($this->conn);
$this->inbox();
}
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];
}
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 4: 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.";
}