2

Hello I am saving email address from a textarea in a database. Now i want this data to be put in an array so I can send each email addres a mail.

On every line break there is an e-mail adres like so:

<textarea>
my@email.com 
my2@email.com 
my3@email.com
</textarea>

This data is save in the database as LONGTEXT. I get this data out of the database as a String. But I want the data to be put in an array. So each email gets an unique index. Note that no special characters like '\n' and < br > are save in the database.

Thanks in advance!

Joachim Vanthuyne
  • 355
  • 1
  • 4
  • 14
  • It's probably better if you explode these addresses and save them as individual rows in the database. That way, when you send each email to your mail server, you can set a flag to say it's done. Otherwise: what happens if your mail server connection fails halfway through sending - how will you know which have been processed? – halfer May 09 '12 at 15:16

6 Answers6

3

Your question seems to indicate that you're saving the email addresses in the textarea as whole rather than first splitting them up and saving each address separately. You will want to take the latter approach. Why split/validate everytime you read out of the database?

First, split the incoming emails with:

$email_addresses = preg_split('/\r\n|\n|\r/', $_POST['email_addresses']);

The regex allows you to take into account the different end of line characters the different platforms out there use.

Then you can loop over the addresses and validate each with:

filter_var($email_address, FILTER_VALIDATE_EMAIL)

Then save each address in its own row in the database.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
2

Assuming your database field is called email_addresses and the new lines are \n:

$email_addresses = explode("\n", $row['email_addresses']);
webbiedave
  • 48,414
  • 8
  • 88
  • 101
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Yes, as the two nice answers stated above you can use

$email_addresses = explode("\n", $row['email_addresses'];

to accomplish what you need. They did however forget to mention that what you are doing is a bad idea. What you are doing presents a huge secuirty risk with SQL injection. See I can place something like

test@aol.com'; DELETE * FROM employees WHERE 1 OR email = '

in that text box and delete your table if you had a statement setup like so

Select * FROM employees where email = '$email_addresses[0]'

A Simple solution is to use 'mysql_real_escape_string($string)' on each of your email addresses, or a regular expression such as

^[A-Za-z0-9._+-]+@[A-Za-z0-9.-]{2,}+\.[A-Za-z]{2,4}$

Try these resources to get you on the right track to prevent your whole DB from being attacked:

How can I prevent SQL injection in PHP?

http://php.net/manual/en/security.database.sql-injection.php

http://bit.ly/ICJhZJ

Community
  • 1
  • 1
tcables
  • 1,231
  • 5
  • 16
  • 36
  • This is a bit of a straw man answer. OP didn't even mention how he's saving the data or if he's using parameterized queries. – webbiedave May 09 '12 at 15:49
  • Look at how he phrased how he is storing the data. if your just taking the raw text and throwing it in the DB then pulling it back and parsing it by delimiters... you didn't go the extra mile to sanitize it. – tcables May 09 '12 at 17:49
0

What have you tried?

Try this :

<?php
$array = explode("\n", $your_long_text);
adrien
  • 4,399
  • 26
  • 26
0

To be safe you should do this:

<?php
    $array = explode('|', str_replace(array("\n","\r"),'|',$your_long_text));
    foreach ($array as $k => $v)
         if (empty($v)) unset($array[$k]);
?>

This covers all types of line breaks, and removes any empty entries that could arise.

Kristian Williams
  • 2,275
  • 22
  • 25
0

I retought my problem and followed the advice of webbiedave. I first split the email addresses and check if they are valid. After that I save every entry as a row in the DB.

// Delete previous Emails
        DB::_execute("DELETE FROM `%s` WHERE education_id = ?", array(self::_getEducationEmailAddressesTable()), array($action['id']));

        // Only save new emails when email is not empty
        if( ! empty($data['emails'])) {
            // save Emails 
            $email_addresses = preg_split('/\r\n|\n|\r/', $data['emails']);

            foreach($email_addresses as $email) {
                if( ! Utils::_isEmailAddress($email)) {
                    return new Error("GiveValidEmailAddress");
                } else {
                    DB::_execute("INSERT INTO `%s` (education_id, email_address) VALUES (?, ?)", array(self::_getEducationEmailAddressesTable()), array($action['id'], $email));    
                }
            }
        }
Joachim Vanthuyne
  • 355
  • 1
  • 4
  • 14