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()
Related
I'm trying to use sendgrid to send emails from my app. To add email addresses via php, according to the sendgrid docs, you use the following:
$email = new SendGrid\Email();
$email
->addTo("example#email.com")
where addTo is used for each email address. I would like like to generate the addTo list dynamically, for example I have a list of 3 emails in a php variable that I would like to send mails to:
$email_addresses = 'example#email1.com, example#email2.com. example#email3.com';
I've tried the following to echo out individual ->addTo properties per email address however it doesn't work:
$email = new SendGrid\Email();
$email
$string = $email_addresses;
$string = preg_replace('/\.$/', '', $string); //Remove dot at end if exists
$array = explode(', ', $string); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
echo '->addTo("'.$value.'")<br>';
}
Does anyone know what I'm doing wrong here?
There's a setTos method in sendgrid-php that handles arrays: https://github.com/sendgrid/sendgrid-php#settos
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]);
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']
}
I have an array of emails and want to send an email for each entry in the array but currently it only sends it to the first address. I have looked around and can't see what I am doing wrong.
//This is the array
$emails = array($teamleademail, $coleademail, $sponsoremail, $facilitatoremail, $cisupportemail);
//Now the foreach statment
foreach ($emails as $value) {
//pulls in email sending code
require_once ("Mail.php");
//variables required to send the email
$from = "Scope Sheet Process Database (no reply)<scopesheetprocess#goodrich.com>";
//Make the $to variable that of the email in the array
$to = "$value";
$subject = "Scope Sheet $scopesheetid Closed";
$body = "Scope Sheet $scopesheetid has been closed and can no longer be edited.";
//send email code
require_once ("sendemail.php");
}
I am using pear php to send the email because of IT issues but that shouldn't matter since it should be running the scripts each time and sending separate emails.
The problem is this line:
require_once ("sendemail.php");
...should just be
require ("sendemail.php");
As it is, it will only be included on the first iteration of the loop, hence only the first email is sent.
This on its own may not fix your problem - if it doesn't we'll need to see the code in sendemail.php.
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");