Contact form submit script - HTML email problem - html-email

I have a contact form script and when the user submits it, they get an email and I get one. But there's a problem with the "message" textarea in the HTML email. It puts it all in one line.
See how I type it out:
And then in the email sent to me (HTML) its all in one line:
As you can see it is all in one line. How can I get it to not be in this line?
This is my code:
<?php
// load the variables form address bar
$subject = $_POST["subject"];
$message = $_POST["message"];
$from = $_POST["from"];
$name = $_POST["name"];
$verif_box = $_POST["verif_box"];
// remove the backslashes that normally appear when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);
$name = stripslashes($name);
$emailContent = "Hello Nathan,
".$name." is trying to contact WeeBuild Support. Here's what they submitted:
<br /><br />
<div style='background-color:#ccc;padding:10px;border:1px solid grey;'>
Name: <strong>".$name."</strong>
<br /><br />
Email: <strong>".$from."</strong>
<br /><br />
Subject: <strong>".$subject."</strong>
<br /><br />
Message:
<br /><br />
<strong>".$message."</strong>
<br /><br /><br />
Their IP Address: <strong>".$_SERVER['REMOTE_ADDR']."</strong>
</div>
<br /><br />
To email them back, simply reply to this message.";
$emailContents = "Hello ".$name.",
Thank you for contacting WeeBuild Support! This email is to let you know that we have received your support request and that we will reply soon.
For your record, here is what you submitted:
-----------------------------------------------------------------------------
Your Name: ".$name."
Your Email: ".$from."
Subject: ".$subject."
Message:
".$message."
-----------------------------------------------------------------------------
In the meanwhile, make sure to add support#weebuild.biz to your contact list/safe senders list so our emails don't end up in your junk folder.
We will be with you shortly!
Kind regards,
WeeBuild Support Team
www.WeeBuild.biz";
$emailContent = stripslashes($emailContent);
$emailContents = stripslashes($emailContents);
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("nathan#weebuild.biz", 'WeeBuild Contact Form: "'.$subject.'"', $emailContent, $headers);
mail($from, 'Thank you for contacting WeeBuild Support!', $emailContents, "From: support#weebuild.biz");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else if(isset($message) and $message!=""){
// if verification code was incorrect then return to contact page and show error
header("Location: index.php?name=$name&subject=$subject&from=$from&message=".urlencode($message)."&wrong_code=true");
exit;
} else {
echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html><head>
<title>Access Denied</title>
<style type=\"text/css\">
body {
font-family: Arial, sans-serif;
}
</style>
</head><body>
<h1>Access Denied</h1>
<p>This page cannot be accessed directly. The needed variables to submit were not provided.</p>
</body></html>";
exit;
}
?>
Note: $emailContent is the email that goes to me and $emailContents is the email that goes to the user.
I tried using the str_replace() and that just caused parsing problems and I could not get that to work. I'm sure I wasn't using it right.
Can someone help me with this? Any help is highly appreciated.

use nl2br();
So edit this snippet
Message:
".$message."
to
Message:
".nl2br($message)."

You can use nl2br() function on $message to change newlines into <br /> tags.

Related

link is not showing in email body in php

I am trying to to use a link in my email body but failed to do that, Whenever i send an email, email is sent but it contain only the plain text but i also want to print a link so that that link will redirect me to another page.
here is my php code,
<?php
include("admin/config.php");
$email=$_POST['email'];
/// mail to customer
$to=$email;
$subject="Thanks for Contacting with us";
// Message
$message = '
<html>
<head>
<title>Request to Change Password</title>
</head>
<body>
<p>You have requested to change password <br> Please follow the below link to change your password.<br><br></p>
<p>Change Password<br><br></p>
<p>Regards<br></p>
<p>http://www.example.com<br></p>
<p>9999999999<br></p>
</body>
</html>
';
$from = 'MIME-Version: 1.0' . "\r\n";
$from.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$from.= 'From: <no-reply#abc#gmail.com>' . "\r\n";
$mail =mail($to,$subject,$message,$from);
if ($mail == true){
header('location:forgot_password.php');
}
?>
Please help me out, thanks in advance
The link is not "showing" because you wrapped it in a <p> tag which is for text (paragraph) only. Wrap the link in a <a> tag and set the href attribute accordingly :
<a href="your link">
A descriptive texte or your link again
</a>

mail(): SMTP server response: 550-"JunkMail rejected

I Would like to send email using contact form in my company website,
I was set the email configuration in php, here is my form :
<form action="sendmail.php" method="post" class="form form--contacts">
<input type="text" name="name" class="form__input" placeholder="Name" required>
<input type="email" name="email" class="form__input" placeholder="Email" required>
<input type="text" name="subject" class="form__input" placeholder="Subject" required>
<textarea class="form__textarea" name="text" placeholder="Text"></textarea>
<input class="form__btn" name="send" type="submit" value="Send">
</form>
and here is sendmail.php :
<?php
if($_POST['send']){
$admin = 'myname#mydomain.co.id';
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
$subject = htmlentities($_POST['subject']);
$text = htmlentities($_POST['text']);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$pengirim = 'From: '.$name.' <'.$email.'>';
if(mail($admin, $subject, $text, $headers)){
echo 'SUCCESS: Mail Successfully sending';
}else{
echo 'ERROR: Error Sending Email';
}
}else{
header("Location: index.php");
}
?>
There might be few reasons for junk mail detection.
First, you don't have "To: " in your headers.
Second, you rely on your system mail and you have no control over from which account you are sending mail from. If your "From:" header is different than your actual "mail from:" in your SMTP connection it's a red flag for spam filter.
You should try using PHPMailer instead of built in function. ( https://github.com/PHPMailer/PHPMailer ) or other alternative.
Some info may be found in this post: Sending email with PHP from an SMTP server

Using PHPmailer all mail landed in spam folder

I am using smtp auth then also mail going into spam folder.
If the body of mail contains an external file (file_get_containts) then the mail is going in the Spam folder.
but, if the body of mail contains only string then the mail is going in the inbox folder.
Can someone please help me out with this?
here is my code:-
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['message']) ){
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$m = nl2br($_POST['message']);
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'info#example.in';
$mail->Password = 'nsdfdk^^dsfx7wffdsry8e^';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'info#example.in';
$mail->FromName = 'John Smith';
$mail->addCustomHeader('MIME-Version: 1.0');
$mail->addCustomHeader('Content-Type: text/html; charset=ISO-8859-1');
$mail->addAddress('example#gmail.com', 'Jay Senghani');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "New Enquiry from website";
$message = file_get_contents('emails/admin.html');
$patterns = array();
$patterns[0] = '/{name}/';
$patterns[1] = '/{email}/';
$patterns[2] = '/{number}/';
$patterns[3] = '/{message}/';
$replacements = array();
$replacements[0] = $name;
$replacements[1] = $email;
$replacements[2] = $phone;
$replacements[3] = $m;
$message = preg_replace($patterns, $replacements, $message);
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent /n';
}
}
// For User Automated Email
if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone'])){
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'info#example.in';
$mail->Password = 'ndfgk^dfgg^gfdggfdgdfgdfx7wfy8e^';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'info#example.in';
$mail->FromName = 'John Smith';
$mail->addAddress($email, $name);
$mail->addCustomHeader('MIME-Version: 1.0');
$mail->addCustomHeader('Content-Type: text/html; charset=ISO-8859-1');
$mail->isHTML(true);
$mail->Subject = "Thank you for your interest Website ";
// $mail->addAttachment('Attachment Path', 'pdf');
$message = file_get_contents('emails/user.html');
$message = preg_replace('/{name}/', $name, $message);
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent /n';
}
}
?>
Here is my admin template:-
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="emailWrapper">
<div id="emailHeader">
<div class="topBar"></div>
<a class="branding" href="http://example.com/" target="_blank" >
<img src="https://i.imgur.com/JME5efdRs.png">
</a>
</div>
<div id="emailContent">
<h2 class="greetings">
Dear Admin,
</h2>
<div class="content">
<p class="intro">
New enquiry from XYZ Website
</p>
<p>
<strong>Name :</strong> {name}
</p>
<p>
<strong>Number :</strong> {number}
</p>
<a class="email">
<strong>Email :</strong> {email}
</a>
<p>
<strong>Message :</strong> {message}
</p>
</div>
<div class="regards">
<h5><strong>Thanks & Regards,</strong></h5>
<h6>XyZ</h6>
</div>
</div> <!-- END #emailContent -->
<div id="emailFooter">
<div class="bottomBar">
<p>
© 2018 Xyz. All rights reserved
</p>
</div>
</div>
</div>
</body>
</html>
Troubleshooting email deliverability is tricky mostly due to the lack of details about the inner workings of anti-spam features at the main email service providers (Gmail, Msn, mail.com, Yahoo etc.). The main aspect of good email deliverability is usually your domain reputation. If you are just starting out with a new email domain most receiving services tend to be sceptic towards your first xx number of emails.
If your email contains invalid or poor html it will up the spam score. If you add attachments to it will certainly up the score.
I figure your emails without attachments are just below the threshold for ending up in the spam folder. When adding an attachment it gets just above the same threshold.
Most services also apply per-user rules, so the handling of your emails can be treated differently between receivers within the same service.
As a step in troubleshooting if PHPmailer is responsible for the poor delivery, I suggest setting up an email client like Mozilla Thunderbird and sending some emails from there.
That will help you figure out the source of the deliverability of your emails.
If you want to avoid thinking about deliverability you can spin up an account with SMTP services like Mailgun.

php email script not working correctly

I have a form on my website:
<form name="contact" action="contact.php" method="post">
<label for="name">Name:</label><br/><input type="text" name="name" id="name"><br/>
<label for="email">Email:</label><br/><input type="text" name="email" id="email"><br/>
<label for="comment">Question:</label><br/><textarea name="comment" id="comment"></textarea><br/>
<input type="submit" value="Send" id="submit">
</form>
This is the script it submits the data to:
<?php
header("Refresh: 3;url=http://www.xyz.com/");
if(isset($_POST['email'])) {
$email_to = "xxxx#gmail.com";
$email_subject = "Enquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comment'])){
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$comment = $_POST['comment']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($comment) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Comment: ".clean_string($comment)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
For some reason the script outputs that the message was sent successfully but when I check my inbox there is no new emails. I don't know what is going on, can anyone help me?
Mail function has errors turned off
#mail($email_to, $email_subject, $email_message, $headers);
remove # from this and add simple if
if(mail($email_to, $email_subject, $email_message, $headers))
{
echo 'mail was sent'; //success message here
}
else
{
echo 'there were errors during sending mail'; //error message there
}
When you find out what errors occur then you can remove them. It can be problem with configuration of SMTP server.
You can set some settings affecting mail() function in php.ini
Read more here.
Moreover, this Thank you for contacting us. We will be in touch with you very soon. message is shown always it doesn't check if something is wrong it is just shown.

Sending email in contact page not working

I am using following code, copied from a website, for a contact page. But I am not getting any emails and the attached files don't find their way to my webserver folder /var/www/. Am I missing something here?
I am running this code on localhost.
email.php
ob_start();
$to = 'baltusaj#gmail.com';
/*$name = $_POST['name'];
$email = $_POST['email'];
$confirm_email = $_POST['confirm_email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$hidden = $_POST['hidden'];
$from = $email;
*/
$keys = array('name', 'email', 'confirm_email', 'subject', 'comments', 'hidden');
foreach($keys as $key)
{
$$key = isset($_POST[$key]) ? $_POST[$key] : null ;
}
print ('
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Email</title>
<script type="text/javascript">
/*<![CDATA[*/
function progress(){
intWidth = parseInt(document.getElementById("container1").style.width) + 1;
if(intWidth <= 400){
document.getElementById("container1").style.width = intWidth+"px";
}else{
document.getElementById("container1").style.width = 0;
}
setTimeout("progress()",300);
}
/*]]>*/
</script>
</head>
<body>
');
//Make sure email and confirm email are the same
if (!empty ($hidden)) {
if ($email == $confirm_email) {
}else{
$email = '';
$confirm_email = '';
}
}
//Do a reg_ex check on the email
if (!empty ($hidden)) {
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*#([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
if (eregi($regexp, $email))
{
}else{
$email = '';
$confirm_email = '';
}
}
// End of email checking
if (empty ($hidden)) {
print ('<div id="container2">
<h1 id="content_h1"><a name="text"> E-mail </a></h1> <p>Please use the following form to e-mail us:</p>
');
include ("form.php");
print ('</div>
<div id="container0" style="display: none;">
<p style="font-size: 15pt; font-family: sans-serif; color:#fd6700; background:#fff;">
Loading...
</p>
<div id="container1" style="width:0px; height:5px; background-color:#fd6700; margin-top:0px; text-align: left;"></div>
<p>Please be patient while your data is processed. This may take a few moments especially if you are uploading a file.</p>
</div>
');
}
if (!empty ($hidden)) {
if ($_FILES['fileatt']['error'] == 1){
print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1>
<p>The maximum file size that can be uploaded using this form is 2 megabytes.
</p>');
}elseif ( (!empty ($name)) && (!empty ($email)) && (!empty ($comments))&& (!empty ($subject))) {
// Get html message content
$form_data = "<p>This email is from <span class=\"bold\">$name</span> \n\n ";
$form_data .= "<p>$comments</p>";
$message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" .
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" .
"<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n" .
"<head> \n" .
" <meta http-equiv=\"content-type\" content= \n" .
" \"text/html; charset=iso-8859-1\" /> \n" .
"<style type=\"text/css\"> \n" .
"body { font-size: 9pt; font-family: verdana, sans-serif; color: #000; background:#fff; } \n" .
".bold { font-weight: bold; } \n" .
"</style> \n" .
"</head> \n" .
"<body>$form_data \n" .
"</body> \n" .
"</html> \n\n";
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the html message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
//We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}else{
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the html message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
}
//That completes the modifications necessary to accommodate a file attachment. We can now send the message with a quick call to mail:
// Send the message
mail($to, $subject, $message, $headers);
$body = "Dear $name, \n\nThank you for your email. We will contact you as soon as possible regarding the matter. \n \n";
mail ($email, "Re: $subject", $body, 'From:you#your_email.com');
print ('<h1 id="content_h1"><a name="text"> Thank you </a></h1> <p>We will contact you as soon as possible. You will receive an automatic e-mail immediately confirming the reception of your email.</p>');
}else{
print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1> <p>Please fill in all the compulsory fields correctly and then resubmit the form. Thank you.</p>');
include ("form.php");
}
}
// This is the end of the insert
print ('
</div>
</body>
</html>
');
ob_end_flush();
?>
form.php
<div id="form">
<form action="email.php" enctype="multipart/form-data" method="post" onsubmit="
document.getElementById('container0').style.display='';
document.getElementById('container2').style.display='none';
progress();
return true;">
<fieldset id="fieldset">
<label for="name">Contact name<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($name)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="name" type="text" name="name" value="<?php print "$name"; ?>" tabindex="1" />
<br />
<label for="email">E-mail address<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="email" type="text" name="email" value="<?php print "$email"; ?>" tabindex="1" />
<br />
<label for="confirm_email">Confirm e-mail<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($confirm_email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="confirm_email" type="text" name="confirm_email" value="<?php print "$confirm_email"; ?>" tabindex="1" />
<br />
<label for="subject">Subject<span style="color: red;">*</span>:</label>
<input <?php if (!empty ($hidden)) { if (empty ($subject)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="subject" type="text" name="subject" value="<?php print "$subject"; ?>" tabindex="1" />
<br />
<label for="comments">Comments<span style="color: red;">*</span>:</label>
<textarea <?php if (!empty ($hidden)) { if (empty ($comments)) { print 'style="background: pink;"'; } } ?>class="form_elements" id="comments" name="comments" cols="19" rows="5" tabindex="1"><?php print "$comments"; ?></textarea>
<br /><br />
<label for="fileatt">Attach document:</label>
<input id="fileatt" type="file" name="fileatt" tabindex="1" />
<br />
<input type="hidden" name="hidden" value="1" /><br />
<label for="submit"><span style="color: red;">*</span> Compulsory fields.</label>
<input id="submit" type="submit" value="Send" tabindex="1" />
</fieldset>
</form>
</div>
For the kajillionth time on this site: Don't build your own mime messages, it's unreliable and painful. Use Swiftmailer or PHPMailer instead. PHP's mail() function is highly unreliable and cannot produce ANY useful diagnostics as to why things are failing. Swift/PHPMailer will both tell you exactly why something blew up.
beyond that, nowhere do you seem to be doing a move_uploaded_file(), so your uploaded files will be auto-deleted by PHP when the script exits. You MUST deal with the files yourself, which means you have to write code to move the files to whatever place you want them stored in.
Are you running an SMTP server on your localhost?