sleepyw Posted November 11, 2015 Share Posted November 11, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/ Share on other sites More sharing options...
requinix Posted November 11, 2015 Share Posted November 11, 2015 1. explode() on commas and foreach 2. Trim extra whitespace 3. If the thing isn't an email then replace space->period and add "@xyz.com" 4. implode() back together into a string 1 Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526237 Share on other sites More sharing options...
sleepyw Posted November 11, 2015 Author Share Posted November 11, 2015 Thank you, requinix. That makes sense, but what will hang me up if the foreach. I don't think I know how to do that...although the rest I think I could manage. Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526240 Share on other sites More sharing options...
requinix Posted November 12, 2015 Share Posted November 12, 2015 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526247 Share on other sites More sharing options...
sleepyw Posted November 12, 2015 Author Share Posted November 12, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526286 Share on other sites More sharing options...
sleepyw Posted November 12, 2015 Author Share Posted November 12, 2015 (edited) 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 November 12, 2015 by sleepyw Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526291 Share on other sites More sharing options...
Barand Posted November 12, 2015 Share Posted November 12, 2015 $recipient is a string containing a single email. You need to implode the array "$exploded_new_recipients" 2 Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526294 Share on other sites More sharing options...
Solution requinix Posted November 13, 2015 Solution Share Posted November 13, 2015 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526308 Share on other sites More sharing options...
sleepyw Posted November 13, 2015 Author Share Posted November 13, 2015 (edited) 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 November 13, 2015 by sleepyw Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526331 Share on other sites More sharing options...
sleepyw Posted November 13, 2015 Author Share Posted November 13, 2015 (edited) Ack - can't edit my above post anymore. Tinkering a little...I think it's working now....will report back. UPDATE: yes - it's working! Thank you both for your assistance! Edited November 13, 2015 by sleepyw Quote Link to comment https://forums.phpfreaks.com/topic/299447-mail-issue-leads-to-need-for-complex-str_replace/#findComment-1526332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.