Jump to content

PDO mail with multiple addresses from db


clint

Recommended Posts

Good day all,

I am trying to create a form where you fill it in and submit. When its submitted to db it selects email addresses from db and mails all addresses in that table to  let them know there was a new post. Below is what I have. It inserts into db and sends mail to test server but not to addresses in table so both functions are working separately but I am struggling to tie them together. How do I go about combining the 2 to make it work? Thank you in advance!

public function mailing_list() {

		$query = $this->db->prepare("SELECT address FROM `emails`");
		try{
			
			$query->execute(); 
			return $query->fetchALL();
			foreach ($query as $row) {    $address = $row['address']; }

		} catch(PDOException $e){

			die($e->getMessage());
		}

	}
	
	public function insert_newscast($topic, $content) {
	
	$query 	= $this->db->prepare("INSERT INTO `news` (`topic`, `content`, `date`) 

		VALUES (?, ?,NOW()) ");

		$query->bindValue(1, $topic);
		$query->bindValue(2, $content);

		try{
			$query->execute();
			$to = implode(", ", $address);
			$subject = $topic;
			$message = '
<html>
<head>
  <title>Notification from test site</title>
</head>
<body>
  <p>New post up on the Website!</p>
  <a href = "http://www.test.co.za">Take a look</a>
  <p>Regards,<br />The Team!</p>
</body>
</html>
';
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			mail($to, $subject, $message, $headers);

		}catch(PDOException $e){
			die($e->getMessage());
		}
		
	}
Link to comment
Share on other sites

I don't quite understand. Are there users in your table with email addresses or is there just a separate mailing list table that contains emails? If so, what you want would be to store the user id in the subscription table if the user is subscribed. If there are multiple mailing lists also store the list id in the subscription table. Then you would just mail the list right there in your insert function. I feel like I need to see a little more code, if there is any.

 

Edit: Mainly, what I need to know is how email addresses are stored.

Edited by kwollaston
Link to comment
Share on other sites

I can't find a way to edit my post, but would this work for you?

	private _addresses;

	public function insert_NewsCast($topic, $content) {
		$query = $this->db->prepare("INSERT INTO `news` (`topic`, `content`, `date`) VALUES (?, ?, NOW())");
		$query->bindValue(1, $topic);
		$query->bindValue(2, $content);
		
		try {
			$query->execute();
			$query = $this->db->prepare('SELECT `address` from `emails`');
					try {
						$query->execute();
						$result = $query->fetchAll();
						foreach($result as $row) {
							$addresses = (empty($addresses)) ? $row['email'] : $addresses .= ', ' . $row['address'];
						}
					}
					catch (PDOException $e) {
						die($e->getMessage());
					}
					
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$message =  '<html>' .
								'<head>' .
								'	<title>Notification from test site</title>' .
								'</head>' .
								'<body>' .
								'	<p>New post up on the Website!</p>' .
								'	<a href = "http://www.test.co.za">Take a look</a>' .
								'	<p>Regards,<br />The Team!</p>' .
								'</body>' .
								'</html>';
					mail($addresses, $topic, $message, $headers);
		}
	}
Link to comment
Share on other sites

I don't quite understand. Are there users in your table with email addresses or is there just a separate mailing list table that contains emails? If so, what you want would be to store the user id in the subscription table if the user is subscribed. If there are multiple mailing lists also store the list id in the subscription table. Then you would just mail the list right there in your insert function. I feel like I need to see a little more code, if there is any.

 

Edit: Mainly, what I need to know is how email addresses are stored.

Hi, thanks for the reply. Sorry if my explanation wasnt very clear. I am trying to keep it as simple as possible so there are 2 tables excluding the user table. There is only one user in the users table.

 

Table 1: news

This table has the following columns: id, topic, content, date

 

Table 2: emails

This table has 2 columns: id and address (id is unique, auto incrememnt and the address is just the email address such as that@this.com)

 

There will only be a few addresses in the emails table and these will get added on another page.

There is a insert form to insert into the news table which worked fine and it sent a mail to mail server but with no "to address"

 

I hope this clears things up.

 

Thanks again for your time. Super appreciated!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.