Pain Posted February 9, 2014 Share Posted February 9, 2014 Hello. I am building a website where I need to send welcome emails. Welcome email text is stored in the database, so i first have to retrieve it, then send. Now I came across this issue - how do I make my code treat the text from the database AS a variable(s). public function retrieveConfirmationEmail() { $this->email_tag = 'Room Confirmation'; $query = $this->db->prepare('SELECT * FROM emails WHERE email_tag = ?'); $query->bindValue(1, $this->email_tag); $query->execute(); $data = $query->fetch(); $message = $data['message']; $subject = $data['subject']; $this->message = $message; $this->subject = $subject; } public function sendConfirmationEmail() { $headers = "From: admin@homelocator.com" . "\r\n"; $headers .= "Reply-To: admin@homelocator.com". "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; mail($this->email, $this->subject, $this->message, $headers); } The first method retrieves 'Message'. That message has to contain PHP variables such as $this->name, $this->title etc. Any advice? thx Link to comment https://forums.phpfreaks.com/topic/286069-php-variable-in-a-database/ Share on other sites More sharing options...
ginerjm Posted February 9, 2014 Share Posted February 9, 2014 IMHO - I would design my db differently. You're not going to put "variable" data into your "static" message, are you? You s/b creating a table of users with their info separate from the messages that you wish to send. Then, you pick a message and the users and do your mail generation. Unless you are sending totally unique messages to each person every time you send out mail, which I don't see working in a db-driven environment. Link to comment https://forums.phpfreaks.com/topic/286069-php-variable-in-a-database/#findComment-1468313 Share on other sites More sharing options...
Pain Posted February 9, 2014 Author Share Posted February 9, 2014 I'm sorry I should've been more clear on this. Each email has to be different. Please take a look at the full version of my class. <?php class Advert { private $db; public $title; public $description; public $postcode; public $picture; public $email; public $date_posted; public $cost; public $message; public $subject; public $email_tag; public $link; public $id; public function __construct($database) { $this->db = $database; } public function setDetails($title, $description, $postcode, $picture, $email, $date_posted, $cost) { $this->title = $title; $this->description = $description; $this->postcode = $postcode; $this->picture = $picture; $this->email = $email; $this->date_posted = $date_posted; $this->cost = $cost; } public function insertAdvert() { $query = $this->db->prepare("INSERT INTO rooms (title, description, postcode, picture, email, date_posted, cost) VALUES (?, ?, ?, ?, ?, ?, ?)"); $query->bindValue(1, $this->title); $query->bindValue(2, $this->description); $query->bindValue(3, $this->postcode); $query->bindValue(4, $this->picture); $query->bindValue(5, $this->email); $query->bindValue(6, $this->date_posted); $query->bindValue(7, $this->cost); $query->execute(); } public function setAdvertLink() { $query = $this->db->prepare('SELECT id FROM rooms WHERE title = ? AND description = ? AND postcode = ? AND date_posted = ? AND email = ?'); $query->bindValue(1, $this->title); $query->bindValue(2, $this->description); $query->bindValue(3, $this->postcode); $query->bindValue(4, $this->date_posted); $query->bindValue(5, $this->email); $query->execute(); $data = $query->fetch(); $id = $data['id']; $this->link = 'http://www.home-decorators.co.uk/rooms/uk/london/'.$id; $_SESSION['last_id'] = $id; } public function retrieveConfirmationEmail() { $this->email_tag = 'Room Confirmation'; $query = $this->db->prepare('SELECT * FROM emails WHERE email_tag = ?'); $query->bindValue(1, $this->email_tag); $query->execute(); $data = $query->fetch(); $message = $data['message']; $subject = $data['subject']; $this->message = $message; $this->subject = $subject; } public function sendConfirmationEmail() { $headers = "From: admin@homelocator.com" . "\r\n"; $headers .= "Reply-To: admin@homelocator.com". "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; mail($this->email, $this->subject, $this->message, $headers); } public function redirect() { header('location: success.php'); } } ?> As you can see I'm getting those variables from $_POST. Obviously I could just write a welcome email like this: $this->message = 'Hello ' . $this->name . ' your email is ' .$this->email; But i want to hold this in the db. Hope that's a bit more clear. Sorry I'm really bad at explaining Link to comment https://forums.phpfreaks.com/topic/286069-php-variable-in-a-database/#findComment-1468316 Share on other sites More sharing options...
.josh Posted February 9, 2014 Share Posted February 9, 2014 put placeholders in your email message and then use str_replace to replace the placeholders with the values from the variables. example: $content = "Hello {{NAME}}, your email address is: {{EMAIL}}"; $name = 'your name'; $email = 'yname@gmail.com'; $replace = array( '{{NAME}}' => $name, '{{EMAIL}}' => $email // etc.. ); $content = str_replace(array_keys($replace),$replace,$content); Link to comment https://forums.phpfreaks.com/topic/286069-php-variable-in-a-database/#findComment-1468320 Share on other sites More sharing options...
Pain Posted February 9, 2014 Author Share Posted February 9, 2014 On 2/9/2014 at 9:22 PM, .josh said: put placeholders in your email message and then use str_replace to replace the placeholders with the values from the variables. example: $content = "Hello {{NAME}}, your email address is: {{EMAIL}}"; $name = 'your name'; $email = 'yname@gmail.com'; $replace = array( '{{NAME}}' => $name, '{{EMAIL}}' => $email // etc.. ); $content = str_replace(array_keys($replace),$replace,$content); Link to comment https://forums.phpfreaks.com/topic/286069-php-variable-in-a-database/#findComment-1468326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.