PHP MySQL how pass value of variables into email body using placeholders - php

I am building an emailing template system using PHP. The idea is that the same system can be used to send a variety of automated email, eg when people join a site etc. The contents of each email are held on a MySQL database and I use placeholders to represent dynamic elements such as name, email, etc. The placeholders take the form of two underscores before and after a name: __name__, (NB the stackoverflow markup may convert the underscores to make the placeholder name appear in bold here) and a separate field matches the placeholder to the variable it represents eg:
__name__=[($user['name'])], __email=[($user['email'])]
When I construct the email, the system takes the placeholders and holds them in an associative array:
$placeholders( '__name__' => $user['name'], '__email__' => $user['email'] );
This is exactly how the array appears. I have used explode to strip out the enclosing [( and )] brackets.
I am able to loop through the email content and use Parsedown and preg_replace to swap the placeholders with their corresponding variables, eg:
$body = 'Dear __name__, your email is __email__'
becomes:
$body = 'Dear $user['name'], your email is $user['email']
However, this is exactly how the content appears in the final email that is actually sent. The value of these variables (which I have successfully established previously in an array $user ) do not appear in the final text, eg:
'Dear John, your email is john#smith.com'.
How do I get PHP to replace the variables with their values before the email is sent?
PS I have also tried:
'Dear ' . $user['name'] . 'Your email is ' . $user['email']
(ie with an inverted comma and a period to differentiate the variables from the rest of the text ) but no avail. It appears as:
Dear Mr Mercer,
Thank you for registering your details with mercermania.com.
Please verify your email address now
We now need to verify that the email address you supplied when you registered does indeed belong to you. Please click on the following link:
http://www.mercermania.com/activation/?x=' . $user_info['email'] . '&y=' . $user_info['active'] . '
I have done it before, so know it can be done but for the life of me I cannot remember how!
Any help greatly appreciated.
PS I am using PHP Mail::factory to send the email and an email is actually sent, and is received, so that is not the issue.

Related

Using Chrome's "Copy email address" on mailto link returns encoded useless text instead of a correct email address

We have a website with restricted access (only for exclusive members) coded in PHP.
On the Contact page we have a bunch of mailto links, for example:
address#domain.com
If a visitor clicks on one of the links, it does the usual stuff (opens a new email in Thunderbird with the To: field correctly filled in).
The problem is when a visitor right-clicks on one of the links, selects "Copy email address" and then pastes it in the To: field or wherever (even in a document), in which case the result would be:
Name%20SURNAME%20%3Caddress#domain.com%3E
instead of
Name SURNAME <address#domain.com>
I've been searching for a solution for hours and already tried rawurlencode(), urlencode() and other possible tricks, with absolutely no effect.
Can some of you please help me?
Here is the PHP code that generates the link:
<?php echo ''.$email.''; ?>
//where $email is a valid email address
//and $name is plain text (usually two words with a space character between)
I believe you're trying to do something that simply cannot be done.
I understand the idea but... it's not a thing.
I found no docs saying that you can put a name and enclose the email address in <>.
https://developer.mozilla.org/.../a#Creating_an_email_link
https://developer.mozilla.org/.../Creating_hyperlinks#E-mail_links
IETF RFC6068
You can of course do:
address#domain.com
I found this : https://stackoverflow.com/a/25854290/3376036.
It seems it's actually not supported but it usually works

How do I set a PHP variable to an array correctly

I am using Swift Mailer for PHP and there are times where there are multiple email addresses to send to. Having inherited a database that has several email addresses per one cell only split by comma's it is not feasible for this to change this to correct standards.
When I use the following in the sendTo field in Swift Mailer, it works just fine.
$email = array('name#email.com', 'name#email.com', 'name#email.com'); // Works
However the email addresses in the database are in the form of
name#email.com,name#email.com,name#email.com
What I tried is the following.
$dbemails = "name#email.com,name#email.com,name#email.com"; // Fetched
$dbemailsrp = str_replace(",", "', '", $dbemails); // Replace , with ', '
$dbemailscp = "'".$dbemailsrp."'"; // Add front and back '
$email = array($dbemailscp); // Doesn't work
Then I tried to put the $email in swift mailer it does not work after going through this process, but works if I manually set up the array as shown in the first block of code.
Any ideas why building the array this way does not work?
Your code does not create an array, it creates an array with a single item as a string. You need to split up the email addresses into an array, you can do this using the explode function, ie:
$emails = explode(",", "name#email.com,name#email.com,name#email.com");

Email Form first name capital

I am using dreamweaver and have a form on my page. I managed to collect details from the user and email them etc but would like it to have the punters name first letter in capital.
$MailSubject = "".((isset($_POST["pename"]))?$_POST["pename"]:"") ." Sent Email From space see Website.";
I have had a look around and have tried adding
ucfirst($foo);
but that didnt work.
can someone please advise me as to how to add the ucfirst to the mail subject variable.
thank you
Seemore

php mail html format link remains inactive

I wanted to make a mail function in php to let visitors create and activate a user account. For this I made a mail with a link which refers to the page that activates the account. Now the problem is that some people want to use characters that interfere with the code inside the email. for example: " " and ' '. I tried to escape these characters, but when such character appears, the link becomes inactive. The mail is sent, but the link is unclickable.
This is what the code looks like.
The variables are set in PHP
$New_user->Username = $db->real_escape_string($_POST['un']);
$RawUn = $_POST['un'];
$New_user->Password = $_POST['pw'];
$New_user->Email = $_POST['em'];
$CheckEmail = explode("#", $New_user->Email);
$New_user->Country = $_POST['cn'];
$New_user->City = $_POST['ct'];
//$NEW_USER IS AN OBJECT CREATED TO HOLD ACCOUNT INFORMATION SUCH AS USERNAME AND EMAIL
//$RAWUN IS A VARIABLE TO HAVE AN UNESCAPED VALUE OF THE USERNAME TO INSERT IN THE INPUT FIELD IF SOMETHING WENT WRONG
After checking the values, the mail is sent:
$message = array(
'Hello ' . $New_user->Username . ',<br/>',
'<br/>',
'Welcome to MakeAMemo.<br/>',
'To start working with your account you will have to activate it.<br/>',
'Just click on the link and you are ready to go.<br/>',
'Log in and check if it works. If not, please contact us(E-mail is on the website).<br/>',
'Your password: ' . $New_user->Password . '<br/>',
'<br/>',
'Kind regards,<br/>',
'<br/>',
'Administration');
$header = array(
'From: makeamemoofficial#gmail.com',
'Reply-To: makeamemoofficial#gmail.com',
'Content-type: text/html');
mail($New_user->Email,"MakeAMemo => New account",implode("\r\n", $message),implode("\r\n", $header));
I have made a connection to the datebase, so the escaping using $db->real_escape_string works fine.
The location of the link will be changed when the website is finished.
I checked if the code worked without the str_replace in the href. No succes. Neither I got succes trying to not escape the username.
The tags are invisible in the mail, so it is recognised. The link is not blocked, because it does work when I don't use special characters. When changing the double quotation marks into single quotation marks, you reverse the effect, which means that instead of " ", ' ' don't work.
I do not think the headers have something to do with it, because the link does work when using normal characters.
Any idea what the cause of my problem is?
Every answer is appreciated.
adear11: here is the generated tag:
link
"s avonds is an incorrect dutch word that contains some of the characters that need to be tested.
Rather than using str_replace in your email, you should use urlencode http://php.net/urlencode
This function is specifically for encoding strings for use in urls
As for the link not always working when it is formed properly, would be that the user isn't using HTML email.
Also, while not specific to your problem, this script is crazy insecure. You never ever ever need to use user supplied input ($_POST in your case) without sanitizing the input first. At a minimum, all of those assignments need to be run through htmlspecialchars.
Update
Given the trouble that you are having, I would consider not passing the actual data around in the URL. Rather, I would save the data to the DB and then generate a token to put in the url. If you generate a token with uniqid you won't have any trouble with these special characters because the string will be alphanumeric. Once the user clicks the link, just grab the data associated with the token and proceed as you would if the data was in the URL.

How to use textarea as input to check duplication and invalid?

My idea is to use a textarea to let the user copy email name address etc.... to it.
The copied data must have delimiter between each value.
However, my questions are:
How can i detect the delimiter used in copied data?
How to manipulate? Store each into different array according to the location? but what if there is some error between eg. if one row has one more entry eg. email name address adress2 when other are email name address
Actually i am doing some process from outlook express export txt file or data copied from excel sheet
For those outlook express export file, there are some spacing for each email that without name .So the problem is occur eg.
aa#aa.com name1 bb#bb.com cc#cc.com name2
Thanks for your kindly help.
You would use explode for this (http://php.net/manual/en/function.explode.php).
If you can ask the users to enter each piece of data on a new line, you can then split the textarea contents by the \n character:
e.g.
$myarray = explode("\n", $textarea_str);
Then each element of the array can be split by the delimiting comma:
foreach ($myarray as $row)
$eachline[] = explode(",", $row);
Then validate the individual items that you've extracted from the delimited data as if they have come individually.
You have to explode(',', $_POST['emails]), then run trough that array and check/trim/validate-email all elements for proper format.
you can pre inform the user ,email address must be separated by (ur delimitor may be comma and so).On php side just explode(delimitor,$_REQUEST['text_area_name']);
Oh, nice question . You are trying to find what is the delimiter used in the copied text . I cannot say a perfect solution, but you may create an algorithm like this .
Consider possible delimiters ex : , ' " | etc.
Explode the text with each delimiter in consideration . count the number of elements returned in each array
For , which delimiter you got the higher number of elements in array , may be used by the user
You can fine tune the code
This is a simple example .
Thanks

Categories