Jump to content

Mail issue leads to need for complex str_replace?


Go to solution Solved by requinix,

Recommended Posts

I'm having a probably server-side issue that I cannot fix, so I need to find a workaround. I have a page designed to allow people to enter their report and email it to a list of people (it will vary each time).

 

There are 2 ways people can enter an email recipient: full email address or first and last name (which is how you find people in the corporate Outlook Exchange accounts). So when the recipient field is entered, it may contain a mix of full email addresses or just first and last names (separated by commas).

 

For example, here's my original array of email recipients:

$recipient = "bob.smith@abc.com, John Smith, Barb Johnson";

The problem is with the first and last names. When the email sends, it adds the entire domain of the server to the email address. For example, if the server I'm using is "test.server.xyz.com" and I enter "John Smith", when the email gets sent, it sends to "John.Smith@test.server.xyz.com" instead of just "John.Smith@xyz.com".

 

So I'm wondering if there's code that would search an array using mixed email addresses, locate just the ones with first and last names, and fix them.

 

How would I code this so it ended up as:

$new_recipients = "bob.smith@abc.com, John.Smith@xyz.com, Barb.Johnson@xyz.com";

Thanks in advance!

 

Note: I've asked IT if they can fix something on the server end to correct this, but I'm not counting on their help, so looking for a workaround.

So you've got $new_recipients. explode() that into a new variable. It'll be an array, so you can foreach over it to get the various bits inside.

foreach ($exploded_new_recipients as $key => $recipient) {
Each $recipient bit will be an email address or name. If you exploded on just a comma (which I suggest) then there could be some spaces too that need to be trimmed off. Once you have the "plain" value, you need to tell if it's a name or email. The easy way to check is to see if there's an @ sign, given that the user should really only be entering names or email addresses. (You could really scrutinize them if you wanted to, though.)

 

For email addresses you'd just leave them alone. For names you'll want to modify them to be email addresses instead: update $recipient with the new value (replace spaces and add "@xyz.com), then update the original array too with

$exploded_new_recipients[$key] = $recipient;
(Because updating $recipient won't also automatically update $exploded_new_recipients.)

 

After all that, the array should be just email addresses. implode() it back together (using comma+space this time, for a more nicely formatted list) and you're back to a single string of everything.

  • Like 1

Thank you again for taking the time to reply! Let me see if I can piece this together (I'm not super proficient with PHP). If I run into issues, I'll post the code I'm working on and hopefully you will still be around to point me in the right direction. :)

Well, I have to admit I'm lost and getting an error on the implode function (although I'm sure most of the rest of this is probably wrong, too). Here's what I have (including comments to show what I'm trying to do). The current $recipient will likely have semicolons separating each name/email address, which explains the explode ';'...

$exploded_new_recipients = explode(';', $recipient);
   // separate each item into new var $recipient
      foreach ($exploded_new_recipients as $key => $recipient) 
        { 
	// remove beginning and end white space from each
	   $recipient = trim($recipient);
	// if item does not include @, replace the space with a '.' and add end email domain		   
           if ((strpos($recipient,'@')) == false) {
	   $new_recipients = str_replace(" ", ".", $recipient) . "@xyz.com";}
	   else {$new_recipients = $recipient;}
	// update var
	   $exploded_new_recipients[$key] = $recipient;
	}
   // put string back together with ', ' between each value
      $final_email_list = implode(', ', $recipient);
Edited by sleepyw
  • Solution

You also need to be putting $new_recipient back into the array, not the original $recipient.

 

Another thing. Keep in mind that strpos() can return 0 if the string starts with a @. And 0 == false. So you'd get something like "@foo@xyz.com". The alternative is "@foo" (looks like an email so don't change it), which isn't good either but it would probably be better to keep that. So use === false for an exact comparison.

  • Like 1

OK, thanks again to both of you! I think I made the changes you both suggested. It's not likely someone would start an email address with the "@" symbol, so I'm not too concerned with that.

 

Good news is I'm not getting the implode error anymore.

 

Bad news is the result is coming out wonky. As a test, I typed in the names John Smith and Lisa White. The out put was "John.Smith,.Lisa.White@xyz.com". And if there is any email address with "@" in it, it ignores the whole function and just uses the original string as-is.

 

So obviously, I'm missing something that separates each recipient--it's checking the entire string. Do I need to put [$key] in the strpos and/or str_replace?

 

Here's what I changed the code to:

$exploded_new_recipients = explode(';', $recipient);
   // separate each item into new var $recipient
      foreach ($exploded_new_recipients as $key => $recipient) 
        { 
	// remove beginning and end white space from each
	   $recipient = trim($recipient);
	// if item does not include @, replace the space with a '.' and add end email domain		   
           if ((strpos($recipient,'@')) == false) {
	   $new_recipients = str_replace(" ", ".", $recipient) . "@xyz.com";}
	   else {$new_recipients = $recipient;}
	// update var
	   $exploded_new_recipients[$key] = $new_recipients;
	}
   // put string back together with ', ' between each value
      $final_email_list = implode(', ', $exploded_new_recipients);
Edited by sleepyw
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.