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;
Related
new to the forum and trying my hand at some coding to help out a friend who cant seem to get this right..
with that being said, i was originally having issues with the contact form on a website not being able to send an email. after a bit of searching on the net and adding a few components here and there it is finally working, but now i have an additional problem..
when the email gets sent through (this is a business website and it sends an email from there to the relevant sales department etc) its all shoved into one line and looks terrible..
like this-
"Name: TEST EMAIL FROM WEBSITETel: 011 937 0572Email:
myemail#zmail.comMessage: TEST PLEASE CONFIRM
test 38"
what i have not been able to find is how to seperate the text so that it comes through in an email as seperate lines like this-
Name: Client
Tel: 011 000 0000
Email: myemail#zmail.com
Message: text here etc.
i have linked the contact form to a .php document, so i would presume that it is in this document that the change would need to take place?? i have tried to add in <br /> & <div> but neither do anything except give me an error message when i try send form from the site.
here is the code for the contactgenie.php linked to the contact form that works 100%
<?php
$EmailFrom = "myeamail#zmail.co.za";
$EmailTo = "myemail#zmail.co.za";
$Subject = "Company Name - Online Enquiry";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "There has been an error, please make sure you entered a correct email address."; // You can edit this to your own error message
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "";
// send email
$success = mail($EmailTo, $Subject, $Body, $Headers = "From: <$EmailFrom\r\n");
// redirect to success page
if ($success){
print "Thank you, your email has been sent! We will be in touch shortly!"; // You can edit this to your own success message
}
else{
print "There has been an error, please make sure you have entered your details correctly."; // You can edit this to your own error message
}
?>
any help with this would be greatly appreciated, thanks
Dillon
You could pre-format the mail body using standard line breaks within double quotes like this:
$Body = "
Name: {$Name}
Tel: {$Tel}
Email: {$Email}
Message: {$Message}";
To achive the double line-height you insert another return between the lines:
$Body = "
Name: {$Name}
Tel: {$Tel}
Email: {$Email}
Message: {$Message}";
thanks for all your help guys! i got it to work, i added "\n" into the code and it works perfectly, i just need it to seperate the text so that it is easier to read when the email comes through from the site, thanks again really helped me out with this!
add <br/> tag to add new line space with your variables like this
$Body = "";
$Body .= "Name: ";
$Body .= $Name.'<br/>';
and wherever else you want to add new line space
for this your must be in HTML form
because there are two type of email html and richtext. takecare of that also.
Not sure if this works but if it's HTML email, this should work.
$Body .= '<html><body>';
$Body .= "Name: ";
$Body .= $Name;
$Body .= "<br>";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "<br>";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "<br>";
$Body .= "Message: ";
$Body .= wordwrap($Message, 50);
$Body .= '</body></html>';
The message may or may not work. Don't get your hopes up to high on that part.
add <br/> tag to add new line space with your variables like this
$Body = "Name : " $Name ."<br/>";
$Body .= "Tel : " $Tel ."<br/>";
$Body .= "Email : " $Email ."<br/>";
$Body .= "Message: " $Message ."<br/>";
Don't use break tag, it is not working.You can easily use "\r\n" as line break
$Body = "Name : ". $name ."\r\n"."Email : " . $email ."\r\n"."Contact : " . $contact;
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>";
// 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);