简体   繁体   中英

Add a "counter" variable to send in mail() vía PHP

Alongside with the name, email, message, etc. I want to send a field that'll show up in my mailbox with an incremental number. For instance:

Name: Alex
Mail: alex@...
Message: Hi.
Email Number: 0001 (then 0002, 0003 and so on) <-- this is what I'm aiming to. Is it possible?

I didn't understand your question fully, but you might be looking for something familiar.

$number=0001;
$msg="Hi. "."Email Number: ". $number;
mail('example@example.com', 'My Subject', $msg);

If you want the number to be unique, enter the letter before sending it to mysql database, with the attribute "unique autoincrement", and then send with this unique number. For example:

<?php
$host = 'localhost';
$database = 'db';
$user = 'root';
$password = 'admin';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($host, $user, $password, $database);
$mysqli->set_charset('utf8mb4');

$stmt = $mysqli->prepare("INSERT INTO email_list (email, subject, message, date) VALUES (?, ?, ?, ?)");
$stmt->bind_param('ssss', $email, $subject, $message, $date);

$message = "Hello";
$email = "caffeinated@example.com";
$date = date('m/d/Y', time());
$subject = "Hello";

$stmt->execute();
$stmt->close();

mail($email, $subject." - ID: " . $stmt->insert_id, $message);
$mysqli->close();
?>

SQL:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

CREATE TABLE IF NOT EXISTS `email_list` (
  `id` int(11) NOT NULL,
  `email` text NOT NULL,
  `subject` text NOT NULL,
  `message` text NOT NULL,
  `date` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;

ALTER TABLE `email_list`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `email_list`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM