I am piping email that get's sent to my server to a PHP script. The script parses the email so I can assign variables.
My problem is once in awhile somebody will include my email address to an email that's getting sent to multiple recipients and my script will only get the first one. I need it to find my email address then assign it to a variable.
Here is what the email array looks like: http://pastebin.com/0gdQsBYd
Using the example above I would need to get the 4th recipient: my_user_email#mydomain.com
Here is the code I am using to get the "To -> name" and "To -> address"
# Get the name and email of the recipient
$toName = $results['To'][0]['name'];
$toEmail = $results['To'][0]['address'];
I assume I need to do a foreach($results['To'] as $to) then a preg_match but I am not good with regular expressions to find the email I want.
Some help is appreciated. Thank you.
Instead of usin preg_match inside foreach loop you can use strstr like below
Supposing you are looking for my_user_email#mydomain.com use following code
foreach($results['To'] as $to)
{
// gets value occuring before the #, if you change the 3 rd parameter to false returns domain name
$user = strstr($to, '#', true) ;
if($user == 'my_user_email')
{
//your action code goes here
}
}
example:
<?php
$email = 'name#example.com';
$domain = strstr($email, '#');
echo $domain; // prints #example.com
$user = strstr($email, '#', true); // As of PHP 5.3.0
echo $user; // prints name
?>
Actually, you do not need to use a regexp at all. Instead you can use a PHP for statement that loops through your array of To addresses.
$count = count($root['To']);
for ($i=0; $i < $count; $i++) {
//Do something with your To array here using $root['To'][$i]['address']
}
Related
I am trying to send a message with one list with PHP, here is my code:
$packs = $db->QueryFetchArrayAll("SELECT * FROM configmoreg");
foreach($packs as $pack) {
echo '<a>'.$pack['setting_id'].'</a>
<a>'.$pack['config_name'].'</a>
<a>'.$pack['config_value'].'</a>
<br>';
}
mail('example#something.com',"My List",$msg);
How do I make it send only one message with list on it?
For example:
id 00000001
My_name game_over_new
NR_Job 11
type_secure MD5
5 etc...
Build up a string in the for-loop, and send that. Consider:
$msg = '';
foreach($packs as $pack)
$msg .= "<a>${pack['some_key']}</a>";
$subject = "My List";
mail('null#example.com', $subject, $msg);
Meanwhile, I gather you're still learning PHP, so hold on to this next piece of advice until you're ready: don't use PHP's builtin mail() function, use PHPMailer.
Try adding all the variables to an array; then send the array containing the variables.
As you don't know how to use arrays I suggest reading:
https://www.w3schools.com/php/php_arrays.asp
However to get what you want, you'll want something along these lines:
$infoToSend = array($pack['setting_id'], $pack['config_name'], $pack['config_value']);
mail('example#something.com',"My List", $infoToSend);
I'm trying to get addresses from a simple text file, so people who don't understand code can still add/remove or change the adresses.
Phpmailer is working totally fine when I set up an address normally, writing it directly in the code, and even with an array and a foreach.
So at the moment I have this :
mail.php :
//all phpmailer settings on $mail var
$addresses = file('mail.txt', FILE_IGNORE_NEW_LINES);
foreach($addresses as $email) {
echo "$email<br>";
$mail->addAddress($email);
}
mail.txt :
'first#address.com'
'second#address.fr'
The echo does return both addresses but the var doesn't seem to work in the addAddress() line, and I get the usual error :
Mailer Error: You must provide at least one recipient email address.
Thanks for correcting me if I'm wrong or if you know any other solution that could work !
Okay so here is the working code corrected with the help of Waqas Shahid :
mail.php :
//all phpmailer settings on $mail var
$addresses = file('mail.txt', FILE_IGNORE_NEW_LINES);
foreach($addresses as $email) {
$email = trim($email);
$change = array('\n', '\t', '\r');
$email = str_replace($change, "", $email);
$mail->addAddress($email);
}
mail.txt :
first#address.com
second#address.fr
You should remove single quotes first, then get value line by line and remove whitespaces using trim() then remove '\n', '\t', '\r' using str_replace()
Using a PHP script file to filter emails. Everything is working properly, but it seems that if I were to initiate a conversation between a cell phone (Verizon Wireless) and the SMTP server, Verizon changes the format of outgoing messages.
For example, I need to respond to an email using XXX#vtext.com but Verizon will respond with XXX#vzwpix.com which cannot be responded to. So I created the following code to try and remove and replace the #vzwpix.com but it still isn't sending the mail. I know the code is working however because if I change the $from in the mail function to XXX#vtext.com the code works and sends the message.
//Parse "from"
$from1 = explode ("\nFrom: ", $email);
$from2 = explode ("\n", $from1[1]);
if(strpos ($from2[0], '<') !== false)
{
$from3 = explode ('<', $from2[0]);
$from4 = explode ('>', $from3[1]);
$from = $from4[0];
}
else
{
$from = $from2[0];
}
if(strpos ($from[1], '#vzwpix.com') !== false) {
str_replace('#vzwpix.com', '#vtext.com', $from);
}
var_dump( mail( $from, "Hi", "What is the email:"));
I am using var_dump just for developmental purposes by the way.
I did some research and I think the best way to do this maybe is to explode the XXX#vzwpix.com at the # symbol, then run a str_replace('vzwpix.com','vtext.com', $from5); However, that didn't work and then I thought maybe implode it after running a str_replace?
If the bad domains are static, why don't you explode on 'vzwpix' and then implode on 'vtext'? This should replace each of the bad instances in you address string.
You forgot to get the result of your str_replace
$from_with_vtext=str_replace('#vzwpix.com', '#vtext.com', $from[1]);
My following script runs fine when a single email address is entered into the textarea, but once two are entered, no emails are sent. What am I doing wrong?
if($_POST['submit']=='Send Email') {
$email_addresses = explode(",\n", $_POST['email']);
foreach($email_addresses as $email_address){
$email_address = trim($email_address);
send_mail( 'noreply#noreply.com',
$email_address,
'Test Email',
"Hello This Email Is A Test");
}
}
var_dump($email_addresses) results in this
array(1) { [0]=> string(39) "email1#test.com email2#test.com" }
You're using the same variable name twice
foreach($email_addresses as $email_addresses)
so on the second loop, the source is overwritten
Edit:
please post the output of
var_dump($_POST['email']);
var_dump(explode(",\n", $_POST['email']));
It should be:
foreach($email_addresses as $email_address){
$email_address = trim($email_address);
send_mail( 'noreply#noreply.com',
$email_address,
'Test Email',
"Hello This Email Is A Test");
}
Also splitting using explode on ",\n" separator isn't a good idea (people can send ",\r\n" in some cases). Provide multiple fields or use preg_split().
If it doesn't work try var_dump($email_address); after the explode() function to get the information what exactly happens with the input (and so you can see that input is actually correct).
UPDATE: As you can clearly see there is no \n in $email_address. It depends on your HTML form.
For a quick fix just explode(', ', $email_addresses); Also - you missed , in your input, which you require to explode that string.
obviously a#b.com b#c.com is not a valid email, and my psychic powers tell me that this is not the correct way to do this. What if I enter something else than an email address? Try to sanitise the data you receive from user. You can not trust user input blindly.
foreach($email_addresses as $email_addresses){
Means that you are overwriting the source array ($email_addresses) with the first entry in the array, because they are the same variable name. Unfortunately, PHP throw an error on this, so you end up rewriting your array with the first email address, and then prematurely (to your needs) exiting the loop (though this is expected and logical behaviour).
okay, here is the code
while (!feof($text)) {
$name = fgets($text);
$number = fgets($text);
$carrier = fgets($text);
$date = fgets($text);
$line = fgets($text);
$content = $_POST['message'];
$message .= $content;
$message .= "\n";
$number = $number;
mail("$number#vtext.com", "Event Alert", $message, "SGA");
Header("Location: mailconf.php");
}
I am trying to get a message sent to a phone, I have 'message' coming in from a text area, and then I assign it to "$content" then I put "$content" into "$message" and then as you can see, in my mail line, I want the address to be the variable "number"#vtext.com I have the while loop because there are many people in these files I wish to send a message to...
When I change the "$number#vtext.com" to my email address, it sends everything fine, I just cannot seem to get it to send to a phone number, please help!
thanks!
The problem is that fgets includes the newline character as part of the return. So you are effectively sending an email to:
1234567890
#vtext.com
Which of course is invalid. Change your line that reads $number = $number to:
$number = trim($number);
And the rest of your code should function as expected, with the email being received on your phone.
Part of my original answer
I would highly recommend using a (probably paid) service to send SMS messages to phones if you need anything remotely reliable. A quick search yielded a few results:
PHP/SMS Tutorial
A list of 5 ways to Text for free <-- This link is dated, but might still be helpful
You need to append the number variable and the "#vtext.com" literal string together:
mail($number . "#vtext.com", "Event Alert", $message, "SGA");