0

I am trying to design a messaging center for the admin of my site. Basically all they have a is a subject and message line. Then I send those through AJAX to my php file where I set the subject and message variables to what I sent over. My question is how can I write a message where it recognizes breaks in between lines by hitting the enter key or where I can add in a link without having to do

<a href="google.com">Google</a>

My form is very basic.

<form action="" method="post" id="rating_form">
    <input type="text" class="inputbar" name="subject" placeholder="Subject" required>
    <textarea rows="8" cols="50" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
    <label for="contactButton">
            <input type="button" class="contactButton" value="Send Message" id="submit">
    </label>
</form>

As is my email...

$to = ''; 
while ($stmt->fetch()) { 
            $to .= $user_email . ', '; 
}             

            $from = "surveys@example.com"; 
            $Bcc = "surveys_check@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"; 

            // Additional headers 
            $headers .= 'From: ' .$from. "\r\n"; 
            $headers .= 'Bcc: '.$Bcc. "\r\n";  


            if (!empty($user_email)) {  
    if (filter_var($user_email, FILTER_VALIDATE_EMAIL)) {  

        //Should also do a check on the mail function 
        if (mail($to, $subject, $message, $headers)) { 
            echo "Your email was sent!"; // success message 
        } else { 
            echo "Mail could not be sent!"; // failed message 
        } 

    } else {  
        //Invalid email 
        echo "Invalid Email, please provide a valid email address."; 
    } 

} else { 
    echo "Email Address was not filled out."; 
}  

I really want a way to do this because no one else knows code and that would be a pain for them. I just want them to be able to type simple messages and send.

Thanks!

Ralph
  • 307
  • 1
  • 2
  • 14
  • 2
    Use the `nl2br` function to replace new lines to `
    `. http://php.net/manual/en/function.nl2br.php
    – chris85 Sep 17 '15 at 15:26
  • No, I'm saying I want a user to be able to type a message in the text area input field, just like I am now and be able to hit the enter key to add spaces without having to hard code anything. – Ralph Sep 17 '15 at 15:28
  • I'm not following you. Can provide input and expected output? – chris85 Sep 17 '15 at 15:29
  • @fred -ii- This has nothing to do with the `nl2br` function?? – Ralph Sep 17 '15 at 15:30
  • @chris85 I have the form and the email on two seperate pages. The user is not hard coding anything, but simply typing an email. So if you went into your email account and typed Dear, Hi, how are you. You do not need to type a break line. The email does it for you and recognizes there is a break. That is what I want, I do not want the user to have to hard code anything. – Ralph Sep 17 '15 at 15:32
  • http://stackoverflow.com/a/3396177/ – Funk Forty Niner Sep 17 '15 at 15:32
  • Yea? That is exactley what the `nl2br` function does. The enter the user hit is sent to the PHP script as `\n` which this function then converts to `
    ` which a browser/mail client will read as a new line..
    – chris85 Sep 17 '15 at 15:33
  • Then how would I add it to my code so that it doesn't need hard coded? Everything in the link you shared has it hard coded. – Ralph Sep 17 '15 at 15:35
  • Well, if you set the content type of your mail to HTML you need HTML line breaks (nl2br does that for you). If you set the content type to plain text you don't need it. – Gerald Schneider Sep 17 '15 at 15:35
  • It's hard coded but it is on dynamic input so it shouldn't be an issue, right? Do you not have editing privileges to the PHP file? – chris85 Sep 17 '15 at 15:37
  • @GeraldSchneider so would I do something like this? `$message .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";` except how can I make it plan text? – Ralph Sep 17 '15 at 15:41
  • @chris85 I can edit the php file, but there will be different messages all of the time. I am somewhat confused on what you mean. If you mean I can put a line or two of code in and never touch it again then that is perfect, but I cannot change it every email. – Ralph Sep 17 '15 at 15:42
  • @chris85 is there anyway you can show me an example. For instance if my message is "Dear Customer, How are you." Could you show me how I would need to change my code so that the 's are recognized as being breaks in my email? – Ralph Sep 17 '15 at 15:51
  • 2
    Try the manual's examples or the examples in the linked thread. It will change every new line to an HTML line break....or even...`mail($to, $subject, nl2br($message), $headers)` – chris85 Sep 17 '15 at 15:55
  • @chris85 Wow, that was easy. Do you know how I could format links? – Ralph Sep 17 '15 at 16:00
  • Don't know what you mean (blue, green, purple, blinking, large, small, etc.) sounds like a separate issue though. – chris85 Sep 17 '15 at 16:06
  • I just want to be able to add links without having to hard code them. It was the first part to my question. – Ralph Sep 17 '15 at 16:09
  • What is the input? If it is `Google` then it should be a link. That should be an absolute path though (http://google.com). Also use the `@` to tag. – chris85 Sep 17 '15 at 16:14
  • @chris85 I know how to make a link, I just don't know how I can let a user add a link without hard coding it. – Ralph Sep 17 '15 at 16:17
  • Take a look at this thread, http://stackoverflow.com/questions/1960461/convert-plain-text-urls-into-html-hyperlinks-in-php. – chris85 Sep 17 '15 at 16:37

0 Answers0