Php form handler/formatting of email message lost - php

I've got a form on my website and when I added htmlspecialchars function to the php form handler, it seemed I also had to change the character encoding to utf-8 so that letters with accents would come through. Now that I've added $headers, the characters are shown correctly but the formatting of the email message ($message) is lost with no line breaks. This is what my code looks like:
$surname = htmlspecialchars($_POST["surname"], ENT_COMPAT, 'UTF-8');
$firstname = htmlspecialchars($_POST["firstname"], ENT_COMPAT, 'UTF-8');
$address = htmlspecialchars($_POST["address"], ENT_COMPAT, 'UTF-8');
$age = htmlspecialchars($_POST["age"], ENT_COMPAT, 'UTF-8');
$message = "
Website form:
Name: " . $firstname . " " . $surname . "
Address: " . $address . "
Age: " . $age . ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
if (mail("me#me.com", $_POST['firstname'] ." ". $_POST['surname'], $message, $headers)) {
header("Location: ...");
}
I tried to fix the issue with
$message = "Website form\n";
$message .= "Name: " . $firstname . " " . $surname . "\n";
$message .= "Address: " . $address . "\n";
$message .= "Age: " . $age . "\n";
but the problem persists. I also tried to change content-type to text/plain but the email message appeared as an attachment file. I am at a loss and don't know what I'm doing wrong as the formatting is still lost. I'm a beginner with php so any help will be appreciated.

You should try message format like this:
// email message
$message ="";
$message .= 'Website Form:'."\r\n";
$message .= 'Name:'. $firstname ." ". $surname . "\r\n";
$message .= 'Address:'. $address . "\r\n";
$message .= 'Age:'. $age . "\r\n";
Here is complete code:
<?php
$surname = htmlspecialchars($_POST["surname"], ENT_COMPAT, 'UTF-8');
$firstname = htmlspecialchars($_POST["firstname"], ENT_COMPAT, 'UTF-8');
$address = htmlspecialchars($_POST["address"], ENT_COMPAT, 'UTF-8');
$age = htmlspecialchars($_POST["age"], ENT_COMPAT, 'UTF-8');
$to = 'youemail#email.com';
$subject = 'Subject Line';
$from = 'info#example.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// email message
$message = "";
$message .= 'Website Form:'."\r\n";
$message .= 'Name:'. $firstname ." ". $surname . "\r\n";
$message .= 'Address:'. $address . "\r\n";
$message .= 'Age:'. $age . "\r\n";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>

Related

Mail Function is giving false return in PHP

I have tried sending email from a basic html form by php mail() function and It has given me false return
Code:
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$name = $_POST['first_name'];
$name1 = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company=$_POST['lead_object'];
$skype= $_POST['skype_id'];
$to = 'himanshu#webkidukan.com';
$subject = 'Advertiser Form Details';
$from = 'ssing648#gmail.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
$message .= '</body></html>';
$t = mail($to, $from, $message, $headers,$subject);
var_dump($t);exit;
if ($t) {
echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";
}else{
echo "Failed to send email. Please try again later";
}
echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";
echo "<script> window.location.href = 'advertiser.php';</script>";
}
?>
I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.
There are some unwanted PHP tags while appending message. try like this
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';

How to attach .ics to email?

I need to attach Outlook calendar file (.ics) to the email with mail function.
This is what I have now, but it is not working.
<?php
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$companyname = $_POST["company"];
$file = "webinar-041016.ics";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$EmailTo = "mail#mail.com";
$Subject = "New Webinar Registration";
// prepare email body text
$Body .= "Firstname: ";
$Body .= $firstname;
$Body .= "\n";
$Body .= "Lastname: ";
$Body .= $lastname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Company Name: ";
$Body .= $companyname;
$Body .= "\n";
$headers = 'From: mail#mail.com' . "\r\n" .
'Reply-To: mail#mail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$conf_subject = "Registration Complete ...";
$conf_body = "Dear " . $firstname . ",\n\nThank you..." ;
$conf_body .= "Content-Type: text/calendar; name=\"".$file."\"\r\n" ;
$conf_body .= "Content-Transfer-Encoding: quoted-printable\r\n";
$conf_body .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
$conf_body.= $content."\r\n\r\n";
$conf_headers = 'From: web#mail.com' . "\r\n" .
'Reply-To: web#mail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// send email
$success = mail($EmailTo, $Subject, $Body, $headers);
$conf_mail = mail($email, $conf_subject, $conf_body, $conf_headers);
// redirect to success page
if ($success){
echo "success";
}else{
echo "invalid";
}
//Attached is an Outlook calendar invite so that you can plan your day accordingly.\n\n
?>
What should I do here? Is my encoding wrong?
You might have already seen this:
PHP Sending Mail
As a suggestion you might wanna have a look into PHPMailer which narrows down the work you have to do, to a single line of code:
$mail->addAttachment('/var/tmp/file.tar.gz');

PHP: Mail not sent

I am trying on the following code but it keeps saying Mail not sent. How do I find the real issue? Code given below:
$full_name = htmlspecialchars(trim($_POST['full_name']));
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$message = htmlspecialchars(trim($_POST['message']));
$to = "mail#example.com";
$subject = $full_name . ' is interested to have a business discussion with you';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\r\n";
// $headers .= "CC: susan#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>';
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>';
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>';
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>';
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo " Mail Not Sent";
}
Try this code hope this helpful.
<?php
//print_r($_POST);
//$fname = $_POST['fname'];
//$lname = $_POST['lname'];
//$email = $_POST['email'];
//$message = $_POST['message'];
if(isset($_POST['fname']) && $_POST['fname'] != ''){
$fname = $_POST['fname'];
}
if(isset($_POST['lname']) && $_POST['lname'] != ''){
$lname = $_POST['lname'];//phone number
}
if(isset($_POST['email']) && $_POST['email'] != ''){
$email = $_POST['email'];
}
if(isset($_POST['message']) && $_POST['message'] != ''){
$com = $_POST['message'];
}
$to = 'noreply#noreply.com';
$subject = 'Site Visiter.';
// message
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$email,$com);
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <nb158f#gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin#site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k#gmail.com' . "\r\n";
// Mail it
$flag = mail($to, $subject, $message, $headers);
echo '<script>alert("Mail Sent :");</script>';
echo '<script>window.location.href="index.php";</script>';

Sending email headers

I have such code for send email:
$email = $_POST['message_email'];
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
before that I check whether the email is correct so it has to be.
Then I send an email:
$sent = wp_mail($newTo, $subject, $companyName . "\n" . $email . "\n" . $phoneNumber . "\n" . strip_tags($message) . "\n" . $outputMail, $headers);
and it doesn't work. I've tried change headers to:
$headers = 'From: My Name <myname#mydomain.com>' . "\r\n\\";
and then it works. Why my code does't work? I need to provide that email retreived from form.
Try like this:
****Ive edited the code****
$from = "your#mail.com";
$to = "to#mail.com";
$message = "Message";
$subject = 'subject';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
if(mail($from, $subject, $message, $headers)){
//success
}else{
//error
}

sending all data from email with require field

I'm creating a user require form in my site. For this I put some validation on compulsory fields, and when a user fills in the form and presses submit and validation is correct then I receive a email on my email address.
But now I would like all user information in the email, like name, city, budget etc... so what changes do I need to make in my email.php script?
If some fields are not compulsory and the user doesn't fill them in, can they affect my script?
My script is:
<?php
$to = "test#networkers.in";
$subject = "a new requiremnet come!";
$message = "Hi,\n\nyou get a new require";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Sender-IP: " . $_SERVER["SERVER_ADDR"] . "\r\n";
$headers .= "From: " . stripslashes($name) . " <" . $email . ">" . "\r\n";
$headers .= "Priority: normal" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
echo "Your mail was sent successfully";
} else {
echo "We encountered an error sending your mail";
}
?>
and the data I recieve is:
$name = $_POST['fname'].' '.$_POST['lname'];
$email = $_POST['mail'];
$phone = $_POST['ph'];
$country = $_POST['country'];
$pt = $_POST['pt'];
$cwl = $_POST['cwl'];
$dyhyows = $_POST['dyhyows'];
$pb = $_POST['pb'];
$bpd = $_POST['bpd'];
$hdyhau = $_POST['hdyhau'];
You can add fields to the message body by concatenating them like so:
$message = "Hi,\n\nyou get a new require";
$message .= "\n Name: " . $name;
$message .= "\n Email: " . $email;
$message .= "\n Phone: " . $phone;
$message .= "\n Country: " . $country;
$message .= "\n pt: " . $pt;
$message .= "\n cwl: " . $cwl;
$message .= "\n dyhyows: " . $dyhyows;
$message .= "\n pb: " . $pb;
$message .= "\n bpd: " . $bpd;
$message .= "\n hdyhau: " . $hdyhau
Any fields that weren’t filled in by the user will simply be blank.

Categories