PHP variables in HTML string - php

I'm receiving some information from the AJAX form, and I need to generate a HTML email that will use some information from this form.
For example, I want to send something basic like
$msg = '<html><body>
<p>Bla-bla-bla $information</p></body></html>'
How can I insert $information variable into paragraph without concatenation (using it is pretty painful, as the email HTML has pretty complex structure)?

If you want to add vars into variable without concat then maybe you are looking for something like this
$information = "Info";
$msg = "<html><body>
<p>Bla-bla-bla $information</p></body></html>";
use your double quotes
Be aware if your var $information is array like this:
$information['info'] = "Info";
$information['id_info'] = 1;
$msg = "<html><body>
<p>Bla-bla-bla $information[info]</p>
<a href\"somepage.php?id=$information[id_info]\">Link</a></body></html>";
Double and single quotes
$email = 'someone#example.com';
$var = 'My email is $email'; // My email is $email
$var = "My email is $email"; // My email is someone#example.com

There are diffent ways to do it:
1) You can use "."
For example:
$msg = '<html><body>
<p>Bla-bla-bla'. $information.'</p></body></html>';
2) You can use double quotes:
$msg = "<html><body>
<p>Bla-bla-bla $information</p></body></html>";

Related

how to pass variable value in URL via file_get_contents

I want to integrate SMS to my API and try to pass the variables in URL but it says error, no number found.
Please help
$sender = "9999708993";
$reply_message = "This is an auto generated message";
echo $sender;
echo $reply_message;
$lines = file_get_contents('http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender');
echo "<pre>$lines</pre>";
use double quotes rather than single quotes if you want to put variables in:
$lines = file_get_contents("http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender");
Please try this:
$sender = "9999708993";
$reply_message = urlencode("This is an auto generated message");
//I think you need to change these
$loginid='myid';
$mypassword='mypassword';
$mysenderid='mysenderid';
$uri = "http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=$myid&password=$mypassword&senderid=$mysenderid&message=$reply_message&number=$sender";
var_dump($uri);
$lines=file_get_contents($uri);
var_dump($lines);
try cleaning the URL, it might have a new line at the end
trim("$URL");

php send mail to both parties POST collected email address

This is probably an easy question but I am not sure the proper syntax.
I am sending an email to my client - but my client also wants that person to receive the emails once they hit submit.
Currently its a POST php form and it works fine - but need to include the POST-email address that was entered into the form.
$from_email = $_POST['emailAddress'];
$to = 'owen#gmail.com';
AND I have mail($to, $subject, $message, $header..
So how do I rewrite the code:
$to = 'owenpiccirillo#gmail.com';
to have also send to the email submitted in the form? because this works.
$to = 'owen#gmail.com, whatever#aol.com';
but this doesnt work....
$to = 'owen#gmail.com' . $from_email;
Thanks in advance
-O
You need a comma and space in the $to concatenation:
NOT
$to = 'owen#gmail.com' . $from_email;
// results in 'owen#gmail.comwhatever#aol.com'
but THIS
$to = 'owen#gmail.com, ' . $from_email;
// results in 'owen#gmail.com, whatever#aol.com'
If this works:
$to = 'owen#gmail.com, whatever#aol.com';
then what exactly is the issue? The reason this doesn't work:
$to = 'owen#gmail.com' . $from_email;
is because there's no comma separating the values. So it would evaluate to:
$to = 'owen#gmail.comwhatever#aol.com';
which isn't a valid email address.
This is because the . is concatenating the two addresses together. So if you do an echo or print_r you would see something like this:
owen#gmail.comwhatever#aol.com
You need to add a comma:
// Value will be injected into the string with double quotes.
$to = "owen#gmail.com, $from_email";
Alternatively, you can use CC and BCC to send copies.

PHP: Email Parsing - Multiple Recipients

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']
}

load html mail file inside php

I implemented a html email inside a html file. And i have a php file which uses the PHPMailer class to send emails.
What i want to achieve is, i have some text inside the html that should change depends who i send the email.
This is the php file that sends the emails
<?php
// get variables
$contact_name = addslashes($_GET['contact_name']);
$contact_phone = addslashes($_GET['contact_phone']);
$contact_email = addslashes($_GET['contact_email']);
$contact_message = addslashes($_GET['contact_message']);
// send mail
require("class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'ssl://smtp.gmail.com:465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = 'danyel.p#gmail.com';
$mailer->Password = 'mypass';
$mailer->From = 'contact_email';
$mailer->FromName = 'PS Contact';
$mailer->Subject = $contact_name;
$mailer->AddAddress('pdaniel#gmail.com');
$mailer->Body = $contact_message;
if($mailer->Send())
echo 'ok';
?>
And the html file containing a simple html mail implemented with tables and all that standard it need.
I want to ask braver minds than mine which is the best approach to accomplish this. :)
Thank you in advance,
Daniel!
EDIT: right now, in the $mailer->Body i have the $contact_message variable as a text email.. but i want in that body to load an html file containing an html email and i want to somehow change the body of the html email with the text inside this $contact_message variable.
One simple way to go is to have special tokens in your html files that will be replaced by the caller. For example assuming that you have two variables that might dynamically change content, name, surname then put something like this in your html: %%NAME%%, %%SURNAME%% and then in the calling script simply:
$html = str_replace("%%NAME%%", $name, $html);
$html = str_replace("%%SURNAME%%", $surname, $html);
or by nesting the two above:
$html = str_replace("%%NAME%%", $name, str_replace("%%SURNAME%%", $surname, $html));
EDIT
A more elegant solution in case you have many variables: Define an associative array that will hold your needles and replacements for them:
$myReplacements = array ( "%%NAME%%" => $name,
"%%SURNAME%%" => $surname
);
and use a loop to make it happen:
foreach ($myReplacements as $needle => $replacement)
$html = str_replace($needle, $replacement, $html);
Create conditional statements based on the email you want to see to.
Then include that in the tempalted php html email text.
You can also pass the changing values to a function which will implement the above.
If I build a website I usually use a templating engine, something like Smarty... You could write your html mail in a smarty template file. Then you could add the wanted texts based on criteria automatically. Just assign the correct values to the templating engine.
To answer your edit:
function renderHtmlEmail($body) {
ob_start();
include ('my_html_email.php');
return ob_get_clean();
}
In your my_html_email.php file you would have something like so:
<html>
<body>
<p>....<p>
<!-- the body -->
<?php echo $body; ?>
</body>
</html>
And
$mailer->Body = renderHtmlEmail($contact_message);
If you need other variables to pass to the layout/template file, add params to that method, or pass an associative array like so function renderHtmlEmail($viewVars) and inside the function extract($viewVars);
You will then be able to use those vars inside the template, for ex. Dear <?php echo $to; ?>,
You will probably have to change your html file from .html to .php if it isn't already.
That is, if I understood the issue correctly.

PHP foreach loop inside foreach loop

I'm trying to do a foreach loop inside a foreach loop.
I have a form where the user type in some text and hit send. On the server site I loop through an array with email addresses and send out the text message.
Now I also want the user to be able to use variables in the textarea, like $name.
So my loop should first loop through the emails, and then str_replace the userinput $name variable, with the names in my array.
The loop works fine with the email part ($tlf), but not the replace $name part.
Could some spot what I am doing wrong?
$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array("name1","name2");
$test=array("mname1", "mname2");
$subject = "Hello world";
$from = "me#gmail.com";
$headers = "From: $from";
foreach($tlf as $name){
$to = $name. "#gmail.com";
foreach($test as $navn){
$message = str_replace('$navn', $navn, $message);}
mail($to,$subject,$message,$headers);
}
Thanks a lot.
EDIT:
The output is an email sent. Say the user type in "hello $name".
I want it to first loop through the $tlf array, in this case creating 2 emails. This goes in as $to in the first loop. This works.
Now the next loop should recognize the user input "hello $name" and loop through the $test array replacing the user $name variable.
The output would be 2 emails send.
Mail output:
to: name1#gmail.com
message: hello mname1
Mail output:
to: name1#gmail.com
message: hello mname2
Let me know if I need to explain better, its hard for me to explain sorry.
When you do the following:
str_replace('$navn', $navn, $message)
Then all literal occurences of $navn will be replaced (the first, the second, the third, ...). So running through that loop a second time can't possibly replace anything more.
You will need to define two placeholders, or make some distinction, or use preg_replace_callback if you want to declare in which order (or other logic) the possible replacement strings are applied.
If you had told us (but you haven't) you only wanted to replace the first occurence in each iteration, then a normal preg_replace(.., .., .., 1) would work.
Is this what you want?
$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array(
array("email" => "name1", "name" => "mname1"),
array("email" => "name2", "name" => "mname2")
);
$subject = "Hello world";
$from = "me#gmail.com";
$headers = "From: $from";
foreach($tlf as $contact){
$to = $contact["email"] "#gmail.com";
$replacedMessage = str_replace('$navn', $contact["name"], $message);
mail($to,$subject,$replacedMessage,$headers);
}

Categories