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>";
Related
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";
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;
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;
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your#email.com';
$body = HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: thanks.html');
The question is, weather or not you can insert a php if statement inside the html email body or the best way to add it in there. I'm creating a detailed inventory and once submitted, it needs to be email to an email. But I don't want to email the full Inventory (including empty inputs), It should only email the fields that have something other than 0. I was thinking:
if ($armChair > 0) { echo 'Arm Chairs: ' . $_POST['armChair']; } ]
But it doesn't seem to actually work... any ideas?
This is a badly constructed question.
Yes, you can definitely do that but you need to do it in a way that is sane. We can't tell by what you posted here since you just plugged in "HTML" where the body is defined.
$body = "Hi there,\r\n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."\r\n" : "";
$body.= "some more text";
If you mean overwrite a segment of $message, yes you can do that as well using something like machine tags {INVENTORY} or the likes...
$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."\r\n", $message) : "";
which of course requires the string {INVENTORY} somewhere in your $message var
I'm testing a PHP mail form, a very barebones one, found here:
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h3>Thanks for your message</h3>';
//Deal with the email
$to = 'mymail#mail.com';
$subject = 'you have a mail';
$contactname = strip_tags($_POST['contactname']);
$adress = strip_tags($_POST['adress']);
$contactemail = strip_tags($_POST['contactemail']);
$textmessage = strip_tags($_POST['textmessage']);
$boundary =md5(date('r', time()));
$headers = "From: My Site\r\nReply-To: webmaster#mysite.com";
$message = "Name: ".$contactname."\n";
$message .= "Adress: ".$adress."\n";
$message .= "E-mail: ".$contactemail."\n";
$message .= "Message: ".$textmessage."\n";
mail($to, $subject, $message, $headers);
}
?>
The problem is I'm receiving an unwanted slash "\" everytime I write a single or a double quote in my message, so "I'm" appear as "I\'m" in my mailbox.
I know it have to do with the way PHP distinguishes code quotes from only lecture quotes, but I wouldn't know what to add in my form to get it properly running.
Any help is appreciated,
The easiest thing to do is to turn magic quotes off in php.ini,
magic_quotes_gpc=false
If you can't do that, you need to remove slashes like this,
if (get_magic_quotes_gpc()) {
foreach($_POST as $k => $v) {
$_POST[$k] = stripslashes($v);
}
}
you can try stripslashing your message , something like :
$message = stripslashes($message);