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

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/+_

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"}

Bootstrap JQuery posting wrong data elements to PHP mail

I am writing a wedding website with a basic form to get the guest email address, guest name(s), guest dinner option, the number of children they are bringing, and a requested song they would like played. I then want to take these values and send them via PHP mail function to an email address. Here is what I have so far:
Here is the snippet of my javascript. The variables emailAddr, g1_name, and g1_dinner all have the correct values in them when I log it to the console.
var data = {
email : emailAddr,
g1Name : g1_name,
g1Dinner : g1_dinner
};
$.post("email.php", data, function(){
alert("Email Success");
});
Here is the PHP:
<?php
$to = "test#email.com";
$from = $_POST['email'];
$guestName_1 = $_POST['g1Name'];
$guestDinner_1 = $_POST['g1Dinner'];
$message = "Email: "+$from +"\n\nGuest 1: "+$guestName_1+" - "+$guestDinner_1;
$message = wordwrap($message, 70);
mail($to,"Wedding RSVP",$message);
?>
Here is what is actually getting POST'd:
And here is the email I get from my web host:
So my main question is why is what is getting POST to my PHP file different than I am specifying?
And a follow up, why am I getting emailed a 0 and not the string I set?
And is this the right way to do something like this?
Any help is appreciated! Thanks!
PHP 101: String concatenation uses ., you're using +, which is mathematical addition.
'string' + 'string' => generally "0" or wonky integer as result
'string' . 'string' => 'stringstring'

Appending GET parameters on HREF tag with PHP

I'm working on a code like this:
<?php
$id=$_POST['id'];
$url_tag = $_POST['url_tag'];
$url_back = 'https://www.page.example.com/page.php?';
$query='id='.$id.'&url_tag='.$url_tag;
$url = $url_back.$query;
echo 'Look how this url shows up: '.$url;
echo '<a href='.$url.'>Click here</a>';
?>
This is, the page receives two POST parameters. Then prepare a link to https://www.page.example.com/page.php? and I append those two parameters as GET parameters with the ids id and url_tag respectively.
Then I display how the whole link looks like. It shows up correctly, in this case https://www.page.example.com/page.php?id=ID&url_tag=URL_TAG, where ID and URL_TAG are the actual values received as POST parameters.
However, when I click on the 'Click here' link, it redirects me to https://www.page.example.com/page.php?, which is the url without any GET parameter.
Why is that happening and how would I solve it? I've tried to feed HREF with urlencode($url) instead, but it redirects me to an address flooded with undesired characters...
Any idea? Thank you!
Try to replace the last line of your code by this:
echo 'Click here';
It should work.
Try using http_build_query(), it takes care of any URL character compatibility issues for you...
// assuming you've already checked and validated your $_POST parameters
$query = http_build_query(array(
'id' => $_POST['id'],
'url_tag' => $_POST['url_tag']
));
$url = 'https://www.page.example.com/page.php?' . $query;
?>
Click here

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.

Append Var to URL in 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()

Categories