Putting new line feed in the text for an email php - 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 />.

Related

PHP email form with attachments not working

I'm trying to use a PHP email form I found online that supports attachments, with a bit of extra code, but it's not working, I get a message saying "Sent", but no emails (I checked spam already).
Here is the php file:
function multi_attach_mail($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = #mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
$message = "Customer Contact"
. "\n" . "Name: " . $_POST['fullName']
. "\n" . "Email: " . $_POST['email']
. "\n" . "Phone: " . $_POST['phone']
. "\n" . "Current Phone: " . $_POST['currentPhone']
. "\n" . "Address: " . $_POST['address']
. "\n" . "Retailer: " . $_POST['retailer']
. "\n" . "Product Type: " . $_POST['productType']
. "\n" . "Specific Item: " . $_POST['item']
. "\n" . "Purchase Date: " . $_POST['purchaseDate']
. "\n" . "Invoice Number: " . $_POST['invoiceNumber']
. "\n" . "Issue: " . $_POST['issue']
. "\n" . "How did the issue happen?: " . $_POST['how']
. "\n" . "When did the issue occur?: " . $_POST['when']
. "\n" . "Have you done anything to try correcting the issue?: " . $_POST['customerTry'];
$to = 'someEmail#gmail.com';
$from = $_POST['email'];
$from_name = $_POST['fullName'];
//attachment files path array
$files = $_FILES['files'];
$subject = 'Customer Contact From Service Form';
$html_content = $message;
//call multi_attach_mail() function and pass the required arguments
$send_email = multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);
//print message after email sent
echo $send_email?"<h1> Mail Sent</h1><br>".$message:"<h1> Mail not SEND</h1>";
My form looks like this:
<form NOVALIDATE action="processContact.php" enctype="multipart/form-data" id="claimsForm" method="post" name="claimsForm">
..lots of fields...
<div class="form-group">
<label class="control-label claimsForm-field-header" for="customerTry">Photos:</label>
<input type="file" id="files[]" name="files[]" multiple="multiple" />
</div>
<input class="green-btn" id="ss-submit" name="submit" type="submit" value="Submit">
</form>
The function came with the form I downloaded, so I don't think there is a problem with it. I've been playing around with the code and trying different things, but no luck (I'm not a PHP guy).
EDIT:
Based on the suggestion below, I tried using phpMailer, following the example they have, here is my php file now:
/**
* PHPMailer simple file upload and send example
*/
$msg = '';
$message = "Customer Contact"
. "\n" . "Name: " . $_POST['fullName']
. "\n" . "Email: " . $_POST['email']
. "\n" . "Phone: " . $_POST['phone']
. "\n" . "Current Phone: " . $_POST['currentPhone']
. "\n" . "Address: " . $_POST['address']
. "\n" . "Retailer: " . $_POST['retailer']
. "\n" . "Product Type: " . $_POST['productType']
. "\n" . "Specific Item: " . $_POST['item']
. "\n" . "Purchase Date: " . $_POST['purchaseDate']
. "\n" . "Invoice Number: " . $_POST['invoiceNumber']
. "\n" . "Issue: " . $_POST['issue']
. "\n" . "How did the issue happen?: " . $_POST['how']
. "\n" . "When did the issue occur?: " . $_POST['when']
. "\n" . "Have you done anything to try correcting the issue?: " . $_POST['customerTry'];
$to = 'murtorius#gmail.com';
$from = $_POST['email'];
$from_name = $_POST['fullName'];
//attachment files path array
$files = $_FILES['userfile'];
$subject = 'Customer Contact From Service Form';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
// This should be somewhere in your include_path
require 'php-mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom($from, $from_name);
$mail->addAddress($to, 'TEST');
$mail->Subject = $subject;
$mail->msgHTML($message);
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'Photos');
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
I changed the input file field name to 'userfile[]', but I get this:
Warning: sha1() expects parameter 1 to be string, array given in /home/zucora/service.zucora.com/processContact.php on line 35
Warning: move_uploaded_file() expects parameter 1 to be string, array given in /home/zucora/service.zucora.com/processContact.php on line 36
If I change the name to 'userfile' (without []) I get a blank screen and no email.

Made a form that e-mails content, but It looks horrible in an email. Need to make file upload compatible

I'm having trouble making my e-mail look better, such as with CSS because it looks like plain text:
Is there anyway to bold or something with html? I've tried concat with <b> tags but I don't think it works because I'm assigning a variable. How can I make it look better? I'm using WordPress.
Also how can I make it File Upload compatible? Thanks
Here's (part) of my code:
<?php
$message = "You have received a message from " . $fullname . ", See information below:" . "\n" .
"\n\n" . "General:" . "\n" .
"\nFull Name: " . $fullname . "\n" .
"\nEmail Address: " . $email . "\n" .
"\nSubject: " . $Subject . "\n" .
"\nIssue Type: " . $issuetype . "\n" .
"\n\n" . "Hardware:" . "\n" .
"\nOrder Number: " . $ordernumber . "\n" .
"\nOrder Date: " . $orderdate . "\n" .
"\nPhoto: " . $photo;
//php mailer variables
$to = get_option('admin_email');
$subject = "Customer Support Form Subject: " . $Subject . " By " . $fullname;
$headers = 'From: '. $email
?>
<?php
if($_POST["submit"]){
$sent = wp_mail($to, $subject, $message, $headers);
}
?>
It seems you're using wordpress and using wp_mail to send the email. The default content-type based on the wordpress codex page
is text/plain. with that said you won't be able to add styling to it unless you change the content type to text/html using wp_mail_content_type filter or add it to your $headers variable.
Notice that I changed the \n to <br /> and added additional header ($headers = 'Content-type: text/html;charset=utf-8' . '\r\n';) to set the content type.
<?php
$message = "You have received a message from " . $fullname . ", See information below:" . "<br />" .
"<br /><br /><strong>" . "General:" . "<strong><br />" .
"<br /><strong>Full Name:</strong> " . $fullname . "<br />" .
"<br /><strong>Email Address: </strong> " . $email . "<br />" .
"<br /><strong>Subject: </strong>" . $Subject . "<br />" .
"<br /><strong>Issue Type:</strong> " . $issuetype . "<br />" .
"<br /><br />" . "Hardware:" . "<br />" .
"<br /><strong>Order Number: </strong>" . $ordernumber . "<br />" .
"<br /><strong>Order Date:</strong> " . $orderdate . "<br />" .
"<br />Photo: " . $photo;
//php mailer variables
$to = get_option('admin_email');
$subject = "Customer Support Form Subject: " . $Subject . " By " . $fullname;
$headers = 'Content-type: text/html;charset=utf-8' . '\r\n';
$headers .= 'From: '. $email . '\r\n';
?>
<?php
if($_POST["submit"]){
$sent = wp_mail($to, $subject, $message, $headers);
}
?>
Regarding adding attachments please check this post.
https://wordpress.stackexchange.com/questions/50264/using-wp-mail-with-attachments-but-no-attachments-received

PHP only sending one mail() in a page

All of sudden, my PHP code that sends an email after someone registers on a form stopped working. I'm cracking my head trying to figure out what is going on...
I already tested a little piece of code emailing myself and it works.
I observed the console window in Chrome while the page executes, but it doesn't show any errors.
The code sends me an email with the form info and sends an email to the user's inbox telling that the registration was successful.
// SENDING EMAIL
//To me
$line_break = "\r\n";
$emailSender = "subscriptions#mydomain.com";
$emailTOme = "me#mydomain.com";
$cco = "support#mydomain.com";
$subject= "my subject";
$messageHTML = '
<p>Nome.......: ' . $nome . '</p>
<p>E-mail.......: '.$email . ' </p>
<p>Profissão.......: '. $profissao . ' </p>
<p>Endereço.....: '. $endereco . ', ' . $endereco_num . ' </p>
<p>Complemento.....: '. $complemento . '</p>
<p>Bairro.......: ' . $bairro . ' </p>
<p>CEP..........: '. $cep . ' </p>
<p>Cidade.......: ' . $cidade . ' </p>
<p>UF...........: ' . $uf . ' </p>
<p>Telefone.....: ' . $telefone . ' </p>
<p>Celular......: ' . $celular . ' </p>
<p>Inscrição p/.: ' . $evento . '</p>
<p>Valor da inscrição: <b>R$' . $valor_inscricao . '</b></p>';
$headers = "MIME-Version: 1.1" . $line_break;
$headers .= "Content-type: text/html; charset=iso-8859-1". $line_break;
$headers .= "From: " . $emailSender . $line_break;
$headers .= "Return-Path: " . $emailSender . $line_break;
$headers .= "Bcc: " . $cco . $line_break;
$headers .= "Reply-to: " . $emailSender . $line_break;
mail($emailTOme , $subject, $messageHTML,$headers, $emailSender);
//email for the user
$messageHTMLuser = '
<p> Lorem </p>
<p> Ipsum </b></p>';
$headers2 = "MIME-Version: 1.1" . $line_break;
$headers2 .= "Content-type: text/html; charset=iso-8859-1". $line_break;
$headers2 .= "From: " . $emailSender . $line_break;
$headers2 .= "Return-Path: " . $emailSender . $line_break;
$headers2 .= "Reply-to: " . $emailSender . $line_break;
mail($email, 'We recieved your registration', $messageHTMLuser, $headers2, $emailSender);
The problem is that now only the second mail() function works.
The $email on the second function comes from the previous form.
Try to comment out the "BCC" line.
As the current php mail don't support this option (anymore) - apparantly as a way to avoid bcc-form-spamers..
In order to use advanced email functions, you would probably be better off using PHPMailer (or a similar lib): https://packagist.org/packages/phpmailer/phpmailer
Add this to the start of your script and it should show you your errors (if any) at the top of the page.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

MIME email, blank message body

So I tried searching but couldn't find an answer that I was able to apply to my code. I have had a developer updating a page for me and these emails come through on my mobile device, but the body is always blank on my computer email, Thunderbird. The code in question looks like this:
$mailBody = "Dog Registration Form Data" . PHP_EOL . $strDogCName . ";" . $strDogFName . ";" . $strBreed . ";" . $strGender . ";" . $strHt . ";" . $strBdmm . "/" . $strBddd . "/" . $strBdyy . ";" . $strFName . ";" . $strLName . ";" . $strAddr1 . ";" . $strCity . ";" . $strState . ";" . $strZip . ";" . $strCountry . ";" . $strPhone . ";" . $strEMail . ";" . $member_num. ";" . $dog_num;
// injection test before setting message headers
$sender_name = $strFName . " " . $strLName;
$sender_name = injection_test($sender_name);
$sender_email = injection_test($strEMail);
// Set headers and send. This should be moved to a reusable function
$mime_boundary = md5(time());
$headers = '';
$msg = '';
$headers .= 'From: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= 'Return-Path: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= "Message-ID: <" . time() . "cform#" . $_SERVER['SERVER_NAME'] . ">" . PHP_EOL;
$headers .= 'X-Sender-IP: ' . $_SERVER["REMOTE_ADDR"] . PHP_EOL;
$headers .= "X-Mailer: PHP v" . phpversion() . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-Type: multipart/related; boundary="' . $mime_boundary . '"';
$msg .= '--' . $mime_boundary . PHP_EOL;
$msg .= 'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL;
$msg .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL . PHP_EOL;
$msg .= $mailBody . PHP_EOL . PHP_EOL;
$msg .= '--' . $mime_boundary . '--' . PHP_EOL . PHP_EOL;
ini_set('sendmail_from', $sender_email);
$msg = "Thank you for registering with NADAC.". PHP_EOL. " Here is the info you provided." . PHP_EOL ."Callname: ". $strDogCName . PHP_EOL. "Registered Name: " . $strDogFName . PHP_EOL . "Breed: " . $strBreed .PHP_EOL . "Gender: " . $strGender .PHP_EOL . "Height: " . $strHt .PHP_EOL . "Birthday: " . $strBdmm . "/" . $strBddd . "/" . $strBdyy .PHP_EOL . "Owner First Name: " . $strFName .PHP_EOL . "Owner Last Name: " . $strLName .PHP_EOL . "Address: " . $strAddr1 .PHP_EOL . "City: " . $strCity .PHP_EOL . "State: " . $strState .PHP_EOL . "Zip Code: " . $strZip .PHP_EOL . "Country: " . $strCountry .PHP_EOL . "Phone: " . $strPhone . PHP_EOL . "Email: " . $strEMail .PHP_EOL . "Associate number: " . $member_num. PHP_EOL . "Dog Number: " . $dog_num;
$mailSubject = "Thank you for registering.";
$send_status = mail($mailTo, $mailSubject, $msg, $headers);
$mailTo = $strEMail;
mail($mailTo, $mailSubject, $msg, $headers);
ini_restore('sendmail_from');
// should check send_status here and do something - TODO
unset($_POST['submitted']);
// Done with the mail, display confirmation
I'm sure it's something simple that I'm just missing. But I can't find it, and the programmer working on it doesn't seem to believe the issue. I don't believe it's a local issue with my email provider.
Have you tried with Content-Type: multipart/alternative instead of 'multipart/related'?
The weird thing if the double mail() calling and $send_status to unknown $mailTo var

PHP mail() - HTML shows up as an attachment once a file attachment is added

Had finally gotten all the bugs out of this and now they said "Oh, we'll need to add attachments..." So, this sends an html mail with a plaintext version and was doing just swell. Now that I have the attachments arriving the mail clients are showing the plaintext version inline and the html version as another attachment and then a seemingly empty 93 byte file with a name like ATT00248.txt.
Can anyone either bash me over the head from behind or tell me where I am going wrong? I want the HTML inline where available in the mail user interface, the plain text version where HTML is not available, and the single attachment as an attachment.
Any help?
<?php
$template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile'];
ob_start();
include($template);
$html = ob_get_contents();
ob_end_clean();
if (strlen($html) == 0) {
echo "The template at $template did not load.";
exit;
}
$email = $_SESSION['user']->email;
$name = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name;
$from = "$name <$email>";
$subject = unslash($_SESSION['subject']);
$TextMessage = strip_tags(unslash($_SESSION['message']));
$notice_text = "This is a multi-part message in MIME format.";
$plain_text = str_replace(' ',' ', $TextMessage);
if ($_SESSION['attachment']) {
$fileatt = 'files/' . $_SESSION['attachment'];
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$mailtype = 'mixed';
$fileatt_type = "application/octet-stream";
$fileatt_name = $_SESSION['attachment'];
} else {
$mailtype = 'alternative';
}
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$body = "$notice_text
--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$plain_text
--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$html
--$mime_boundary
";
$body .= "Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--$mime_boundary\n";
// #1 //
if ($to = $_SESSION['recipients'][0]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #2 //
if ($to = $_SESSION['recipients'][1]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #3 //
if ($to = $_SESSION['recipients'][2]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #4 //
if ($to = $_SESSION['recipients'][3]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #5 //
if ($to = $_SESSION['recipients'][4]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// CC self? //
if ($_SESSION['cc_me']) {
mail($from, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($from) . ".<br />";
}
if ($fileatt) {
unlink($fileatt);
}
echo "<a href='email_start.php'>Click here</a> to send another email.";
list($_SESSION['email'], $_SESSION['subject'], $_SESSION['bullets'], $_SESSION['message'],
$_SESSION['templateFile'], $_SESSION['template'], $_SESSION['cc_me'], $_SESSION['recipients']) = '';
?>
Pekka had it right - It was simple and robust to use Swiftmailer. http://swiftmailer.org
I'd post this as a comment, but it's too long.
// #1 //
if ($to = $_SESSION['recipients'][0]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #2 ... #3 ... #4 ... #5
Will end up executing all blocks, since ($to = $_SESSION['recipients'][0]) will be always true. It will also display the "Email sent to ..." even when mail() fails.
What you want is:
if (in_array($to, $_SESSION['recipients'])) {
if (mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header)) {
echo "Email sent to " . htmlentities($to) . ".<br />";
}
}
Or, if you really want to mail everyone, or
foreach ($_SESSION['recipients'] as $to ) {
if (mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header)) {
echo "Email sent to " . htmlentities($to) . ".<br />";
}
}

Categories