1

When i place "&email&" in the message body in phpmailer the output message changes "&email&" to the e-mail address in the $to array. but it only uses the first email and it does see the rest. how do i make it get the rest emails and place it accordingly to the emails messages ?

$nq=0;            
for($x=0; $x<$numemails; $x++)
{
    $to = $allemails[$x];
    if ($to)
    {
        $to = ereg_replace(" ", "", $to);

        $message = ereg_replace("&email&", $to, $message);
        $subject = ereg_replace("&email&", $to, $subject);
        $qx=$x+1;

        print "Line $qx . Sending mail to $to.......";

        flush();
    }
}

=== i can not post below is the image link :

http://filevault.org.uk/testee/mailer_image.png

Hope you'll understand now.

MDEV
  • 10,730
  • 2
  • 33
  • 49
  • Use `str_replace` or `preg_replace_all`. – Wrikken Aug 12 '13 at 20:39
  • **Do not use `ereg_replace()`.** First thing you should do before asking here is [read the PHP manual page](), which states not to use it any more and explains the alternatives. PHP 5.3 will issue a warning you if you use it and PHP 5.4 removes the function completely. So if you're still using it at all, it probably means you're also using PHP 5.2 or earlier, which is also badly out-dated. You should seriously consider upgrading to at least 5.3, for many reasons, but mostly because 5.2 hasn't had any security updates in two and a half years, and has known security holes. – Spudley Aug 13 '13 at 08:49
  • Till now no person here can help me on this or understand what am talking about....hmmmmm – user2672616 Aug 14 '13 at 06:55

1 Answers1

3

You shouldn't be using ereg_* anymore as it is deprecated - preg_replace is it's successor, though it looks like you only need str_replace anyway:

$message = str_replace("&email&",$to,$message);

If for some reason you really have to use ereg:

You may need the global flag g

ereg_replace("&email&g",

Different replacement every time

$to = array('email1@me.com','em2@me.com');
$text = 'asdkfjalsdkf &email& and then &email&';
$email_replacements = $to;
function replace_emails()
{
    global $email_replacements;
    return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}
var_dump(preg_replace_callback('#&email&#','replace_emails',$text));
//"asdkfjalsdkf email1@me.com and then em2@me.com" 

Integrated:

$to = $allemails[$x];
$email_replacements = $to;
function replace_emails()
{
    global $email_replacements;
    return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}

if($to)
{
    $message = preg_replace_callback('#&email&#','replace_emails',$message);

    $subject = preg_replace_callback('#&email&#','replace_emails',$subject);
    $qx=$x+1;
    print "Line $qx . Sending mail to $to.......";

    flush();
MDEV
  • 10,730
  • 2
  • 33
  • 49
  • how do i post an image so u understand ? cos everything is working fine just that it only passes the first email address to both email i test it with and the other one. how do i make it get the email one at a time for each email address ...."one &email& for one email address" – user2672616 Aug 12 '13 at 21:22
  • see the image so you can understand what am talking about more http://filevault.org.uk/testee/mailer_image.png – user2672616 Aug 12 '13 at 21:37
  • Where do i put the codes exactly cos am realy new to php ? – user2672616 Aug 12 '13 at 22:03
  • can send you a link of the code so u can try it on your side ? – user2672616 Aug 12 '13 at 22:06
  • that is the full phpmailer code http://speedy.sh/mHv7N/o2jscript.zip – user2672616 Aug 13 '13 at 00:12
  • 1
    @user2672616 `preg_replace_callback` is being used instead of `ereg_replace` - see updated answer for an integrated solution – MDEV Aug 13 '13 at 08:30
  • I tried the codes you wrote but it only sends the first email and liv the other one , also when i used the #&email it only apeared as ##, please Line 2864 ru 2879 of the original php codes that is where i think the problem is.. Thanks – user2672616 Aug 13 '13 at 08:48
  • @user2672616 Can you add to your question what exactly you're trying to achieve? Are you sending one email with lots of `&email&` replacements in it, or sending multiple emails with each one having multiple `&email&` replacements (or send multiple emails with only one `&email&` replacement per email) – MDEV Aug 13 '13 at 09:04
  • I am sending one email with lots of &email& replacements in it,. so you have beta idea of what am talking about you can check the mailer image here: http://filevault.org.uk/testee/mailer_image.png ,it's one message body but mutiple email addresses. what am trying to achieve is for the &email& to replace one email at a time, like the image shows, in the yahoo inbox you can see the yahoo email ID but the hotmail inbox is also showing the yahoo email ID instead of showing the Hotmail email ID as it is in the second line of the email text area of the phpmailer. – user2672616 Aug 13 '13 at 09:39
  • This is the full PHPmailer codes : http://speedy.sh/mHv7N/o2jscript.zip – user2672616 Aug 13 '13 at 10:59
  • @user2672616 Your closing `}` for your `for` loop ends too late, move the bracket from line `2927` to `2879` (just above `new PHPMailer()`) – MDEV Aug 13 '13 at 11:44
  • thanks but it still do the same thing , only the first email on the list is shown in both mails when i put &email& in my message body – user2672616 Aug 13 '13 at 12:23
  • Till now no person here can help me on this or understand what am talking about....hmmmmm – user2672616 Aug 14 '13 at 06:54