-3

I send email to multiple recipients using below code but its has problem. First i have to set all emails on php code ($to="email,email,email more"). Then i can send them email.

I wants a text area box () for paste my all email/email list and send them. I don't want to add email one by one on php code.

This is my Code:

<?php
     $to = $_POST['email_list']; //i'm trying this but not working.
     //$to = "xyz@somedomain.com".","."xyz1@somedomain.com".","."xyz2@somedomain.com";
     $subject = $_POST['subject'];
     $message = $_POST['message']; 
     $header = "From:abc@somedomain.com \r\n";
     $retval = mail ($to,$subject,$message,$header);
     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
  ?>
  <textarea type="text" name="email_list"></textarea><br/>
  <input type="text" name="subject"/><br/>
  <textarea type="text" name="message"></textarea><br/>
  <input type="submit" name="submit" value="Submit"/>
Masud Rana
  • 51
  • 8

1 Answers1

1

If you want to build a list of email recipients, you can use explode() to get the list of emails into an array, and then use implode() to get them into a single string with a comma delimiter.

For example, if you have a textarea with an email address seperated with a new line character (one email per line), you can use PHP's PHP_EOL as the delimiter.

// Get the submited email addresses into an array
$email_list = explode(PHP_EOL, $_POST['email_list']);
// Implode that array into a comma-delimited string
$to = implode(",", $email_list);

If need be, you could run $email_list through a foreach loop or array_walk first, if you want to validate the email addresses, etc.

Edit: See this answer: Explode PHP string by new line.
Rather than relying on PHP_EOL, it may be best to use a regular expression to look for \r and \n. Reason being that EOL is system (server) dependent, while the actual line break character(s) come from the end user's browser, which uses their operating system's EOL.

RToyo
  • 2,877
  • 1
  • 15
  • 22
  • And if you want to use any delimiter other than comma, use $email_list = explode(delimiter, $_POST['email_list']); – Ravinder Reddy Aug 14 '17 at 21:36
  • can you please update full code – Masud Rana Aug 14 '17 at 21:38
  • i am trying like this: `$to = explode(EOL, $_POST['email_list']);` and `$recipients = implode(",", $to);` but not working. i wants to add email on textarea box without comma. – Masud Rana Aug 14 '17 at 21:40
  • @MasudRana I updated the answer to show that you assign the returned string from `implode()` to your `$to` variable. You've almost got it, except it's backwards. The implode() result is what you want for your `$to`. – RToyo Aug 14 '17 at 21:40
  • its showing me notice: `Notice: Use of undefined constant EOL - assumed 'EOL'` – Masud Rana Aug 14 '17 at 21:43
  • @MasudRana It's `PHP_EOL`. – Barmar Aug 14 '17 at 21:50
  • @MasudRana That was my mistake. It should have been `PHP_EOL`, instead of `EOL` as the explode() delimiter. However, after reviewing what PHP_EOL covers, explode() may not be the best case here. Rather, you should use preg_split, as described [here](https://stackoverflow.com/a/3997367/4458445), if you want to separate your email addresses by line. I can update the answer to use preg_split if you intend to use "one email per line". – RToyo Aug 14 '17 at 21:54
  • Its working, thanks @RToyota sir and barmar sir – Masud Rana Aug 14 '17 at 21:58