PHP email, how to include variable in message - php

I have an upload script that I have purchased. I need to add some more functionality to it however and my php knowledge is pretty basic. What I need is for an email containing the file location to be sent out via email to a set address. Basically a notification that something has been uploaded.
I have worked out what part of the code these needs to go in, and have got as far as adding this which works perfectly:
// Send Email Notification
$to = "info#email.co.uk";
$subject = "A Website User uploaded files";
$message = "The download link goes here. ";
$from = "registrations#email.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
The next line of code in the script outputs the value I want to send in the message of the email like this:
$TMPL['message'] .= '<div class="success">Download:
<a href="index.php?a=download&q='.$execLastRow[0].'"
target="_blank">'.$_FILES['fileselect']['name'][$key].'</a></div>';
Obviously this is the wrong syntax but this is the gist of what Im trying to do:
// Send Email Notification
$to = "info#email.co.uk";
$subject = "A Website User uploaded files";
$message = "Download: '.$_FILES['fileselect']['name'][$key].'. ";
$from = "registrations#email.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
Assistance as always is appreciated!

Edit
Appending to an existing string add . like .=
$message .= 'Download: '.$_FILES['fileselect']['name'][$key].'';
#DevZer0 noticed that you need to add $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; to set the content type to HTML.
Before edit
Because you start the string with " and then href="
So the first " in href is closing your string.
$message = 'Download: '.$_FILES['fileselect']['name'][$key].'';
You could compare the row above with yours and check the color syntax.

Your problem is to put variables in a string right?
$message = 'Variable1: ' . $var1 . ', Variable2: ' . $var2 . ', Variable3: ' . $var3;

Change -
$message = "Download: '.$_FILES['fileselect']['name'][$key].'. ";
To
$message = 'Download: '.$_FILES['fileselect']['name'][$key].'.';

You could assign text like this.
$message = <<<HTML
"Download: {$_FILES['fileselect']['name'][$key]}. ";
HTML;

Related

Formatting String within Variable PHP

Is it possible to format the string within a variable within PHP script.
The following script sends an email to the user. The part shown is my entire body of the email including my signature. I want to bold or change the font size of my signature and everything I've tried didn't work.
I tried including within the string, but it just reads the code and not implementing it. Tried to look over all the web to see if there's a possibility, but all the results show echo......
<?php
if(isset($_POST['button_1'])){
$to = $_POST['email'];
$from = 'xxxxx <xxxx#xxxxx.edu>';
$subject = "Thank you for your interest";
$message ="Name" . "\n" . "Work" . "\n" . "Position" .
"\n" . "(xxx) xxx-xxxx" . "\n" . "xxxx#xxxx.edu";
$headers="From:" . $from; mail($to, $subject, $message, headers);
mail($to,$subject,$message,$headers);
}
?>
Trying to format "Name" within $message
You should use html in your text body.
For example, to make text to be bold you should use:
$message = "<b>Your text here</b>";
To enable html in your message your should set these headers:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

passing multiple variables from an email to a php page via url

I am trying to send multiple variables via an email to a php page when the link is clicked on by the user, both the variables display on the email, however I am only able to pass one variable through the URL to the php page. I think this has something to do with my quotation marks
email code
$email = 'example#hotmail.co.uk';
$name = $_POST['name'];
$email_message =$message = $_POST['feedback'];
$date_added = date("y-m-d");
$message = " $email_message,
<a href='http://www.example.co.uk/email_feedback/feedback_result.php?name=$name&message=$email_message>Click HERE to Activate :)</a>";
$headers = 'From: <example.co.uk>' . "\r\n" .
'Reply-To: <example.co.uk>';
$subject ='confirm account';
mail($email, $subject, $message, $headers,
'example.co.uk');
Try changing the $message to:
$message = '$email_message,
Click HERE to Activate :)';
Your href does not have a closing "/'. And hopefully that's the only problem.
$message = $email_message. '
Click HERE to Activate :)';

PHP mail with breaks database

I am pulling information from a database, and trying to send it as an email. There will be multiple rows of data pulled from the database. This is what my code looks like...
<?php
$to = "--";
$subject = "Test mail";
$message .= "This Week's Memo: ";
while ($row = mysqli_fetch_assoc($result)){
$message .= $row["first_name"];
$message .= (date ('F-d ', strtotime($row["time"])));
$message .= $row["title"];
$message .= $row["memo"];
};
$from = "--";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
All of the info mails fine. My problem is, I would like to keep the breaks in. For instance after the title, I would like a break, and then the memo info to start.
Any ideas I could take a look at how to achieve? Thanks for any help.
please let me know if more info is needed. thanks!
Just add it. Concatenate a new line after your title
$message .= $row["title"] ."\n";
As side note I would suggest you to start declaring message variable and then concatenate the other strings
$message = "This Week's Memo: ";
//^ no dot at first declaration
try:
$message .= $row["title"].'<br/>';
$message .= $row["memo"];
use PHP_EOL to work on different OS
$message .= $row["title"].PHP_EOL;

HTML Email using PHP script

I want an email to be sent in HTML and inside this email i need to pass data from php form. The email is sent successfully as an html email but it doesn't display data passed from PHP VARIABLE.
$name = "PHP TEXT";
$headers = "From: " ."mail#example.com" . "\r\n";
$headers .= "Reply-To: ". "mail#example.com" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";
$email_to = 'example#mail.com'; //the address to which the email will be sent
$subject = "test";
$email = "example#mail.com";
if (mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // success
} else {
echo 'failed'; // failure
}
This line:
$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";
it should be like:
$message = "<html><body><h1>HTML TEXT $name </h1></body></html>";
or
$message = "<html><body><h1>HTML TEXT " . $name . " </h1></body></html>";
<?php and ?> is used to specify that text inside it is a PHP code, not HTML. Here are some details about that.
But you are already in PHP code block and instead of specifying another PHP block, you should use string concatenation.
you aren't using any of the form values in your script. do you know how $_POST or $_GET work?
you are setting $name to a hardcoded value, so it will always be that value when you use it, and also, since you are creating the string with PHP, you don't need to echo php from within this line. i.e. (since you are using double quotes)
$message = "<html><body><h1>HTML TEXT $name;</h1></body></html>";
so, if you can post your HTML form, your $name assignment will probably be something like
$name = $_POST['name']
but you are strongly encouraged to sanitize and validate all user supplied info. It should be a fairly simple Google search.
An alternative solution can be closing the " before like
$message = "<html><body><h1>HTML TEXT ".$name." how are you?</h1></body></html>";
No need for the starting and closing of PHP as you are already in PHP workspace.
Try this..
$message = "<html><body><h1>HTML TEXT".$name."</h1></body></html>";
You are assigning the html to a php variable. So you don't have to echo the variable there.. Just use proper concatination
The $message variable itself has been used wrongly.
$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";
It should be either
$message = "<html><body><h1>HTML TEXT $name </h1></body></html>";
or this:
$message = "<html><body><h1>HTML TEXT ".$name." </h1></body></html>";

PHP mail function user can't receive the mail if I added .com.sg instead of just .com

I want to send the user an activation link after they registered an account. when I put this http://www.homeloan.com.sg in the $message I didn't receive the email, but when I remove the .sg and put http://www.homeloan.com it works. There's no error message, so I really don't know what's my mistake. Please help
here are my codes:
$id = mysql_insert_id();
$to = 'myemail#gmail.com';
$subject = "E-mail Verification";
$message = 'Click on the link to verify your account-> http://www.homeloan.com.sg/rates/activate?id='.$id.'';
$headers = "From: Homeloan Singapore" . "\r\n" . "Reply-To: enquiry#homeloan.com.sg";
mail($to,$subject,$message,$headers, '-f enquiry#homeloan.com.sg');
Make sure if your site have a form to fill, then fill the form correctly by assembling the input tag in the corresponded variable.
Try concatenating 'the email' to the variables ($...) with (.) or "...".
I really don't know what's my mistake
There is no mistake. I tried this code:
<?php
$id = 1;
$to = 'my_email#gmail.com';
$subject = "E-mail Verification";
$message = 'Click on the link to verify your account-> http://www.homeloan.com.sg/rates/activate?id='.$id;
$headers = "From: Homeloan Singapore" . "\r\n" . "Reply-To: enquiry#homeloan.com.sg";
mail($to,$subject,$message,$headers, '-f enquiry#homeloan.com.sg');
It arrived in the mailbox. Maybe, for your account it was put into spam folder?

Categories