Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP Send email with only one language

Oldemar

Coder
I have email marketing with all language:

Code:
<form action="email.php" method="post">
<textarea name="emails" value="emails"></textarea>
<input type="submit" value="Subscribe">
</form>
<?php $emails = $_POST["emails"];
//$email = explode/ltrim/array/string/etc ???
echo $emails;



Code:
All have mixed emails with all language:
(textarea emails)
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
...

But how can I have with one language? example I would have only .de

Can you tell me? I am completey newbie to this.
 
If I understand you correctly you can use explode and array_filter to achieve this.

Use explode to break up the input based on the new line character and then filter out any that don't have a .de extension:

[CODE lang="php" title="Emails"]<?php

$emails = "[email protected]\[email protected]\[email protected]";
$emailsArr = explode("\n", $emails);

$filteredEmails = array_filter($emailsArr, function ($email) {
return preg_match("/\.de$/", $email);
});[/CODE]

You might want to be careful with this, in trusting user input you could end up spamming and having your server ip blacklisted.
 
The code is only designed to filter out emails with a suffix of ".de". If you output (print_r or something) $filteredEmails you should see "[email protected]" in the array not anything else.

The current code has no output which is why it's blank. I helped with the filtering, so now you have the ability to get the emails that you want to send a message to.

$emails btw should come from $_POST['emails'] (though as I said, remember to validate / sanitize the user input)
 
PHP:
<?php
/* for testing purposes
$_POST['emails'] = "[email protected]\[email protected]\[email protected]";
*/
$separator = "\n"; // new line, but could be comma, etc
$extension = ".de"; // the allowed extension

/*
You could also use multiple allowed extensions... use in_array() and a loop or something to check each extension
$allowed = array(".de", ".com.de");
*/

if(isset($_POST['emails']) && strlen(trim($_POST['emails'])) > 0){
    $emails = explode($separator, $_POST['emails']);
    $num_emails = count($emails);
    if($num_emails > 0){
        for($loop = 0; $loop < $num_emails; $loop++){
            $ext_position = strpos($emails[$loop], $extension);
            if($ext_position === false || $ext_position !== strlen($emails[$loop]) - strlen($extension)){
                echo $emails[$loop] . " does not contain or end with $extension<br>";
                continue; // this email does not contain extension, or the extension is not at end of email
            }
            echo $emails[$loop] . " ends with $extension<br>";
        }
    } else {
        // no email list provided
    }
} else {
     // no email list provided
}
?>
 
Last edited:

Latest posts

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom