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";
Related
This question already has answers here:
linebreak in html php email
(2 answers)
Closed 3 years ago.
The form is working, but the textarea input has no new lines.
The whole message is showed on a single line.
FYI, my textarea has no CSS. And I have changed the email adress for privacy purposes.
Thanks in advance,
Sam
<?php
$emailAddress = $_POST["email"];
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$textMessage = $_POST["message"];
$to = "contact#website.com";
/*standard headers for HTML mail*/
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
/*Add. headers*/
$headers .= 'From: ' . $emailAddress . "\r\n";
$headers .= 'Reply-to: ' . $emailAddress . "\r\n";
$headers .= 'First name: ' . $firstName . "<br>";
$headers .= 'Last name: ' . $lastName . "<br><br>";
$headers .= 'Message:'."<br><br>";
$status = mail($to, "New client inquiry", $textMessage, $headers);
if($status)
{
echo "<p>Your mail has been send succesfully!</p>";
}
else
{
echo "<p>Your mail has not been delivered, please try again</p>";
}
?>
You set the content type of mail as text/html, so it expects the content to be valid HTML.
As you should know, newlines have no effect on html rendering. BR or P tags should be used.
Either transform your message in proper HTML (replacing newlines with BR) or change the content type to text/plain.
By the way CSS has nothing to do here (won't alter the content of value sent during request).
I've created a web site and I'm using this link for a JS pop up form to be emailed using PHP.
I also used the code from here and everything works except for a couple things. When I don't remove some variables, the order of information is out of place when it emails.
And when I keep all the variables, I get the following error in a log and nothing sends until I remove them:
PHP Warning: mail() expects at most 5 parameters, 7 given in xxxxxxxxxxxxxxxxxxxxx/quote.php on line 41
Below is the code where the error is coming from:
else{
$Company = $_POST['company'];
$Email = $_POST['vemail'];
$Name = $_POST['name'];
$Number = $_POST['number'];
$Info = $_POST['info'];
$headers = 'From:'. $email2 . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
// $message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("info#bvcdenver.com", $Company, $Email, $Name, $Number, $Info, $headers);
echo "Your quote request has been sent successfuly ! Thank you for your interst. You are being redirected back to xxxxxxxxxxxx in 5 seconds.";
}
How can I send all the variable? Order won't matter if I can get them all to send.
Note: I don't have a lot of scripting experience. This site is created using only HTML/CSS and these PHP and JS sections. So ideally I'd like to not change the entire site.
You need to proper use the php mail function() as it is stated here http://php.net/manual/en/function.mail.php.
The max number of parameters is 5:
- TO ( in your case: "info#bvcdenver.com" )
- SUBJECT ( in your case: $Company )
- MESSAGE ( in your case: $Email )
the last 2 are additional headers and additional parameters.
If you want to send all the data "email, name, number and info" you should organize a string/text variable and put it on the 3rd place
like:
$message = $Email . " - " . $Name . " - " . $Number . " - " . $Info;
mail("info#bvcdenver.com", $company, $message, $headers);
This should do the work and you can customize the message numbers how you want, with html or raw new lines, and get a proper template.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';//pass every variable into message
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
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 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>";
I am trying to set the mail body to display the links in email for the user to download pdf in case if they need to download again in future. It is not html body as it doesn't work on most email showing html codes. So I decide on plain text for email instead.
$umessage .='<a href="'.home_url('/download.php?f=pdffiles/'.$filename).'">'.$title.'';
}}
What is wrong with single quotes? It displays in email:
file name
I don't think double quote will help, right?
EDIT #2
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"
seems that it won't work well in any email application except apple mail.
Any idea on that or maybe it is incorrect?
EDIT #3
$umessage = "Thank you for downloading pdf. \r\n\r\n";
$umessage .= "Please click on the link below to download\r\n";
if(count($selectfiles)>0)
{
foreach($selectfiles as $key)
{
$keyArray = explode('#',$key);
$filename = $keyArray[1];
$title = $keyArray[0];
$umessage .='<p>Download '. '<a href="'.home_url('/download.php?f=pdffiles/'.$filename).'">'.$title.'</p>';
}}
$fm ='xxxx';
$to='xxx';
$subject = "Download Request from ". $name;
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:<'.$fm.'> ' . "\r\n" . 'Reply-To: ' . $to;
$uto = $email;
$usubject = "Thank you for Downloading our PDF";
wp_mail($to, $subject, $message, $headers);
wp_mail($uto, $usubject, $umessage, $headers);
If you're sending the email as plain text, then any HTML formatting you send will be displayed as plain text. It's as simple as removing the html formatting from your email
$umessage .= 'download link: '.home_url('/download.php?f=pdffiles/'.$filename);
Relative links only work in the context of a web page. Within the email, a link of "/download.php?yadda" is meaningless, because there is no host to attach it to.
You can get around this by using full URLs within the links in email:
$umessage .='<a href="'.home_url('http://example.com/download.php?...
You MAY also be able to deal with this using a <base> tag in your HTML, but that may not be interpreted properly by all email clients. You'd need to test.