Linebreaks in PHP mail(): \n, \\n and \r\n dont work - php

This is on a LAMP server.
All the answers I've found online suggest that one of the above should work, but I cant get it working.
My code is:
$to = $email_to;
$subject = 'Website Form';
$message = 'From: ' . $name .
' \r\n Date: ' . $date .
' \r\nText: ' . $text .
'\r\n Use on Presentations?: ' . $presentations .
'\r\n Use on Websites?: ' . $website .
'\r\n Use on Case Studies?: ' . $casestudy;
$headers = 'From: website#website.com' . "\r\n" .
'Reply-To: no-reply#website.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Would anyone know what I've done wrong?

You are using single quotes, if you want to use \n or \r you have to put them in double quotes, like:
$value = "here is some text, and a $variable, and a line end\r\n";
I do a lot of php mailer stuff and \r\n has to be in "..." or it's not executed as an escaped character, but rather as a literal.
Ie:
echo ".\n.";
# is this:
.
.
#but
echo '.\n'.;
#is this:
.\n.
that's all.

For that you can set header like $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; and then user <br /> in place of \n.

Why are you using \r\n, use <BR>
$message = 'From: ' . $name .
' <BR> Date: ' . $date .
' <BR>Text: ' . $text .
' <BR>Use on Presentations?: ' . $presentations .
' <BR>Use on Websites?: ' . $website .
' <BR>Use on Case Studies?: ' . $casestudy;

Related

Send multiple images in email using mail()

I stored the images in url format in database.now i want to send mail with multiple images using mail() in php
$sql11 = "SELECT `photo_id`, `project_id`, `map_id`, `map_flag`, `user_id`, `photo_path`, `pdf_path`, `created_at`, `modified_at` FROM `photos` WHERE map_flag='Task' and map_id='$task_id'";
$rl = $conn->query($sql11);
while ($res11 = mysqli_fetch_assoc($rl)) {
$array[] = [$res11['photo_path']];
}
$str = implode(',', $array);
$message = $message_content . "\n" . "\n" . "\n" . $message1 . "\n" . $message2 . "\n" . $message3 . "\n" . $message4 . "\n" . $message5 . "\n" . $message6 . "\n" . $array . "\n" . $message8 . "\n";
$from = "123#test.com";
$headers = 'From: ' . $from . "\r\n";
$result = mail($to, $subject, $message, $headers);
First off, you need to tell the email app that this is html. This can be done by adding the line
$headers .= "Content-type: text/html; charset=UTF-8";
Next, what is photo_path? Does it also include the full url or a relative url? You need to set the full url i.e. http://example.com/{$res11['photo_path']}

PHP mail() works in one situation and doesn't work in another

I have an userbase. Whenever a new user registers I send them a welcome email through php mail(). It works absolutely fine. Here is the mail part of the code-
$emailto = $useremail;
$toname = $username;
$emailfrom = 'hello#*****.com';
$fromname = '*****';
$subject = 'Welcome to ****';
$messagebody = 'Dear '.$name.'<br/><br/>Thank you for becoming a member in the family of <b>*******</b>.<br>Please enter your chosen <b>Username : '.$username.'</b> and the <b>Activation Code : '.$y.'</b> to complete your registration process.<br/><b/r> You can enter the details in the link: www.*****.com/reg_success.html<br/><b/r><b><i>The*****Team</b></i>';
$headers =
'Return-Path: ' . $emailfrom . "\r\n" .
'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'X-Priority: 3' . "\r\n" .
'X-Mailer: PHP ' . phpversion() . "\r\n" .
'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: 8bit' . "\r\n" .
'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
$params = '-f ' . $emailfrom;
$test = mail($emailto, $subject, $messagebody, $headers, $params);
But almost the same mail() code, which I am using for sending a password reset link. Is not working. Although the code is not throwing an error, the mails are not getting delivered. I have checked that registration mails are working perfect and fast. Not over here. here is the code
$pwrurl = "www.*****.com/reset_password.php?q=".$*****;
$emailto = '$useremail';
$toname = '$username';
$emailfrom = 'hello#****.com';
$fromname = '****';
$subject = 'Password Change Request';
$messagebody = 'Dear '.$name.'<br/><br/>We received a request to reset the <b> login password </b> of your account. <br/>If you have not made the request, please call us immediately as someone else might be trying to access your account. <br> If you have indeed requested the reset, please click on the following link to reset your login password: <br/>'.$pwrurl.'</br>Remeber the link will remain active for an hour.<br/><b/r><b><i>****</b></i>';
$headers =
'Return-Path: ' . $emailfrom . "\r\n" .
'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'X-Priority: 3' . "\r\n" .
'X-Mailer: PHP ' . phpversion() . "\r\n" .
'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: 8bit' . "\r\n" .
'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
$params = '-f ' . $emailfrom;
$test = mail($emailto, $subject, $messagebody, $headers, $params);
It is because of, in second code.
$emailto = '$useremail';
$toname = '$username';
where as in your first code
$emailto = $useremail;
$toname = $username;

Include date, IP and HTTP_HOST in Support form

I am trying to include the current date, senders IP and Host in my contact form submission.
This is the code:
<?php
if( isset($_POST['name']) )
{
$to = 'info#mydomain.com'; // Replace with your email
$subject = 'Contact Form Submission'; // Replace with your $subject
$headers = 'From: ' . $_POST['email'] . "\r\n" . 'Reply-To: ' . $_POST['email'];
$date = date('M jS, Y # h:i a');
$IP = $_SERVER['REMOTE_ADDR'];
$Host = $_SERVER['HTTP_HOST'];
$message = 'Name: ' . $_POST['name'] . "\n" .
'E-mail: ' . $_POST['email'] . "\n" .
'Subject: ' . $_POST['subject'] . "\n" .
'Department: ' . $_POST['department'] . "\n" .
'Message: ' . $_POST['message'] . "\n" .
'Date: ' . $_POST['$date'] . "\n" .
'IP: ' . $_POST['$IP'] . "\n" .
'Host: ' . $_POST['$Host'];
mail($to, $subject, $message, $headers);
if( $_POST['copy'] == 'on' )
{
mail($_POST['email'], $subject, $message, $headers);
}
}
?>
The Email is received, but without Date, IP and Host. Any help would be greatly appreciated.
You have already stored(ip, date etc.) in variables so use these variables for mail. also these values are not exists in POST data so try to replace
'Date: ' . $_POST['$date'] . "\n" .
'IP: ' . $_POST['$IP'] . "\n" .
'Host: ' . $_POST['$Host'];
to
'Date: ' . $date . "\n" .
'IP: ' . $IP . "\n" .
'Host: ' . $Host;
You should attach like this(bellow) because $date, $IP and $Host are not transmitted in a post request:
$createdDate = new DateTime();
$date= $createdDate->format('d.m.Y H:m');
$message = 'Name: ' . $_POST['name'] . "\n" .
'E-mail: ' . $_POST['email'] . "\n" .
'Subject: ' . $_POST['subject'] . "\n" .
'Department: ' . $_POST['department'] . "\n" .
'Message: ' . $_POST['message'] . "\n" .
'Date: ' . $date. "\n" .
'IP: ' . $IP. "\n" .
'Host: ' . $Host;

How to Keep People from Randomly Accessing Form Action Page

I am creating an HTML form that references a PHP file to send an email. However, people keep hitting the form's action page, which then sends me an email with no information filled in. I would like to solve this by somehow protecting that action page from outside hits.
Here is my code:
HTML form:
<form name="homecontact" action="/admin/formactions/writequick.php" method="POST">
<input name="name" placeholder="Name"></input>
<input name="email" placeholder="Email"></input>
<input name="phone" placeholder="Phone"></input>
<textarea name="message" placeholder="Message"></textarea>
<input type="submit" style="background:MidnightBlue; color:white;">
</form>
PHP Script:
<?php
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
?>
The form works perfectly, but I just do not know how to keep people from hitting that action page, unless they go through the form.
"I like that, but will it still send it if only one field is blank? I don't want people to HAVE to put their contact info if they do not want to."
Quickest fix then: name your submit button
<input type="submit" style="background:MidnightBlue; color:white;" name="submit">
and wrap your code inside a conditional statement.
if(isset($_POST['submit'])){
// rest of your PHP/mail code
}
then just use a header to redirect on mail success
...
mail($email, $subjectuser, $messageuser, $headers);
header("Location: http://www.example.com/");
exit;
Use this in your form: (replace it with the one you have now)
<input type="submit" style="background:MidnightBlue; color:white;" name="submit">
Rewrite:
<?php
if(isset($_POST['submit'])) {
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
// redirect after mail sent
header("Location: http://www.example.com/"); // modify it to your site
exit;
}
else{ echo "You can't do that from here."; }
?>
Wrap the code in
if($_SERVER['REQUEST_METHOD']=='POST'){
//Do stuff here
}
This will make sure the user is making a post request before doing anything. Also you will want to verify the variables have values before mailing. Use
if(!empty($_POST['variable_key_here'])){
//Do stuff here
}
you can check $_POST super global array
<?php
if(isset($_POST['name'])) {
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
} else {
die("Access denied");
}
?>
Your code to submit contact details on your mail is correct, but
there is no validation in it.
We have many methods to validate forms :
1.You can use CAPTCHA in your form.
2.Client Side Java Script Validations as well as server side PHP validations to check for correct email pattern , empty field , html special characters and all.
Check this link :
http://www.w3schools.com/php/php_form_url_email.asp
http://www.w3schools.com/js/js_form_validation.asp
There are many methods for validations , just google as per your requirement and your problem will be solved.
You can try this:
<?php
if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST)) {
// you should also validate your _POST data (email syntaxis, etc)
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
}
?>
That way, it doesn't matter if a user goes directly to that page (in GET mode). It will have to go through a post form to actually invoke the mail() function. You could also add a simple additional security layer (to avoid posts requests from other sites) by adding a hidden input in the form:
<input type="hidden" name="hash" value="<?=md5(date("Y-m-d H"))?>">
And then just apply this validation at the start of the writequick.php file:
if($_POST['hash'] == md5(date("Y-m-d H"))) {
I hope that helps!
Try this
<form name="homecontact" action="/admin/formactions/writequick.php?action=mail" method="POST">
</form>
in your writequick.php page
if(isset($_GET['action'])){
//place your mail code
}

Putting new line feed in the text for an email php

I am using the following code
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = "<html><body><p>".$message."</p></body></html>";
$this->Mail($storeEmail, $subject, $message);
function Mail($to, $subject, $message)
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: The Vow Engagement Ring Finder <thevow.engagement#gmail.com>' . "\r\n";
// Mail it
if(!mail($to, $subject, $message, $headers)) {
throw new Exception('There was a problem trying to send an email.');
}
}
The problem is all is i get is one paragraph. I have added <br>s but its like they don't work. The mail I get is simple paragraph without any new line feeds.
Mistake in <br /> tags
$message = $mess0 . "</br>" . $mess1...
^^^^^^
Replace all </br> with <br />
It will not work. You have to use \r\n for line breaks. Also try <br />.

Categories