Append Var to URL in PHP - php

I can't seem to get this to work...what I have is a link being auto-generated to send out in an email, and I need to be able to append the user's email address to the URL, but I can't seem to get it formatted correctly. Here's what I have:
href=\"http://example.com/unsubscribe/\" . $email . \"\"
Where the resulting link in the user's email should be
http://example.com/unsubscribe/myemail#myemail.com
The way the unsubscribe is structured, it wants it the above format instead of with ?email=. Can I do this?
EDIT 12/3/13:
Thanks for all the help, guys! Part of my problem is that I wasn't passing the email var through my generate email function. :/
Here's how I ended up setting it up:
$unsubscribeurl = 'http://example.com/unsubscribe/';
$unsubplus = $unsubscribeurl . $email;
$unsubscribelink = 'UNSUBSCRIBE';
Then to set the link up, I just used $unsubscribelink.

Creating the url:
$url = 'http://example.com/unsubscribe/' . urlencode($email);
Creating the link:
$link = 'link';
Using the link in html context:
<?php echo $link ?>
Or:
link
urlencode()
htmlspecialchars()

Related

how can i append a $name variable inside a $lang variable?

i have a lang file with lots of pre-made statements
the one i'm working one is used as a template to email users
$lang['ADD_STORE_REQUEST_EMAIL_TITLE'] = " New store has been added";
as you guys can probably guessed, the code above is what would be the email title
my question is, how can i put variable in there? say i wanna have the email title as "hello john, new store has been added"
mail(ADMINISTRATOR_EMAIL,$lang['ADD_STORE_REQUEST_EMAIL_TITLE']"From: no-reply#gmail.com");
i tried adding the 'name' variable before and after the $lang, but it wont work. i tried appending it by name+$lang.... but still wont show up
You can do it like this:
$lang['ADD_STORE_REQUEST_EMAIL_TITLE'] = " New store".$yourVar." has been added";
or
$lang['ADD_STORE_REQUEST_EMAIL_TITLE'] = " New store has been added".$yourVar;
You can use the function sprintf to accomplish this.
Also. To concatenate strings in PHP, you need to use . instead of +
Ex:
mail('email#example.com', sprintf('This is a %s day', 'fine'), sprintf('Hello mr. %s', $name);
This will result in an email being sent to email#example.com with the subject "This is a fine day" and body "Hello mr. {some name from the variable"}

Send Facebook image url as parameter

Facebook image url for user profile looks like
https://scontent.xx.fbcdn.net/hprofile-xfa1/v/t1.0-1/c26.26.321.321/s50x50/1004031_160695494109373_202362495_n.jpg?oh=bc24275f3c0b63adcd7f2
I want to send this url in request to server
$FBImageUrl = "https://scontent.xx.fbcdn.net/hprofile-xfa1/v/t1.0-1/c26.26.321.321/s50x50/1004031_160695494109373_202362495_n.jpg?oh=bc24275f3c0b63adcd7f2";
$postString = "http://example.com/script.php?img_url=" . FBImageUrl;
But what will happen if this url have question mark inside?
use urlencode()
$FBImageUrl = "https://scontent.xx.fbcdn.net/hprofile-xfa1/v/t1.0-1/c26.26.321.321/s50x50/1004031_160695494109373_202362495_n.jpg?oh=bc24275f3c0b63adcd7f2";
$postString = "http://example.com/script.php?img_url=".urlencode($FBImageUrl) ;
In your script.php
$img_url = urldecode($_GET['img_url']);
You need to urlencode your parameter.
You can use base64_encode & base64_decode. Also for security reasons image URL wont be visible in the address bar.
$FBImageUrl = "https://scontent.xx.fbcdn.net/hprofile-xfa1/v/t1.0-1/c26.26.321.321/s50x50/1004031_160695494109373_202362495_n.jpg?oh=bc24275f3c0b63adcd7f2";
$base64Url = base64_encode($FBImageUrl);
$postString = "http://example.com/script.php?img_url=" . $base64Url;
Receiver you can decode it
$fbUrl = base64_decode($_GET['img_url']);

Current page url in subject line via mailto?

How to populate current page title (or current url) to the subject line via mailto?
Been using the code below as a starting point, obviously modifying the "Page Title Here" bit, but can't find a solution:
<?php echo "<a href='mailto:test#test.com" . $to . "?subject=Page Title Here" . $subject . "'>Send an email</a>";?>
Set page title to php var as
$title = 'Example';
and use it for
<title><?=$title;?></title>
and mail
<?php echo "<a href='mailto:test#test.com" . $to . "?subject=" . $title . $subject . "'>Send an email</a>";?>
This is not generically possible with PHP. PHP has no knowledge of what the page title is, or your HTML structure at all.
You will have to go to your code where you set the page title, and use that same variable in your e-mail. If this PHP code is handling a post from some page or something, you need that page title posted with your form data (which you can get with JavaScript document.title).

Using CodeIgniter's word_censor() to replace values in a file

I would like to know if this is the best practice with CI or if there is some better way.
In my project I am sending a confirmation email during the registration process. I have a HTML and TXT template. They both consist of something like this:
Hello {USERNAME}, please click on the activation link below to finish the registration. Link: {LINK}
And in my method I open the templates using the file helper and then replace the {} strings with the actual values using the text helper's word_censor() method like this:
$tmpName = '{USERNAME}';
$tmpLink = '{LINK}';
$name = 'Jon' //retrieved from registration from
$link = 'mysite.com/activate/239dwd01039dasd' //runs the activate function providing the unique ID as an argument
$template = word_censor($template, $tmpName, $name);
$template = word_censor($template, $tmpLink, $link);
return $template
Then I just take the $template and put it inside the CI's mail helper like this:
$this->email->message($template);
What I would like to know is if this is the best way to replace contents of html/txt files with my own values or if there is any better and more efficient way to achieve the same result. I just don't like that I am using the word_censor() function to do something other than what it was intended for..
The better way is to store the email template as a view file.
view
Hello <?php echo $username; ?>, please click on the activation link below to finish the registration. Link: <?php echo $link; ?>
Controller
$data['username'] = 'Jon';
$data['link'] = 'mysite.com/activate/239dwd01039dasd';
$email_template = $this->load->view('myfile', $data, true);
$this->email->message($email_template);
Setting the third parameter on view() to true will return the template rather then echo it out.

PHP : mailto function gets breaking when populating text having lines and url

Hi I am creating a task assignment page, in which after creating the task admin can send a mail to assignee to know the task. I cant use PHP mail function since I cant configure office SMTP mail address. So I am using mailto function.
Issue happens when my Commenst got symbols like " &, ", ' . It will stop executing the remaining part of code
In my mail it just populates the first line. So my comments is getting broken.
Here is my code to mailto function :
';
You put everything into one line which brings you into devils kitchen. Instead put the functionality apart (e.g. first load the data from the database), then process the data (e.g. build the mailbox name, the subject and the body) and then finally create the mail URL.
<?php
# load data from database
$assignee = mysql_result($result, $i, 'assignee');
$comments = mysql_result($result, $i, 'comments');
# process data
$mailbox = sprintf('%s#symantec.com', rawurlencode($assignee));
$headers = array(
'subject' => 'Task Assignment',
'body' => "Hello $assignee,\n\nDescription : $comments ...\n\nFor more details log in to ...",
);
# generate the mailto URL
$url = sprintf('mailto:%s?%s', $mailbox, http_build_query($headers));
You can then just output the $url:
printf('<img src="images/mail.png" width="32" height="32" p="center">',
$url);
You need to explicity urlencode everything that is non alphanumeric.
Note, urlencode (the php command) doesn't urlencode everything such as underscore and letters and perhaps other things.
Example:
$test="this: is it's dog/+_";
echo "<br>";
echo urlencode($test);
echo "<br>";
echo $test;
produces:
this%3A+is+it%27s+dog%2F%2B_
this: is it's dog/+_

Categories