user3731438
user3731438

Reputation: 283

phpmailer add more contents to body of email

I am brand new to phpmailer.. i have made some progress by creating a simple form that allows users to send emails to our inbox:

<form id="contact-form" class="contact-form" method="post" action="send-mail.php">          
    <input type="text" name="name" id="name" placeholder="Name" tabindex="1">
    <input type="text" name="subject" id="trade" placeholder="Your Trade" tabindex="3">
    <input type="text" name="email" id="email" placeholder="Your Email" tabindex="2">
    <input type="text" name="number" id="number" placeholder="Contact Number" tabindex="4">
    <textarea id="message" rows="8" name="message" placeholder="Message" tabindex="5"></textarea>
    <div class="grid-col one-whole">
            <button type="submit">Send Your Message</button>
    </div>
</form>

.php:

<?php
require('PHPFiles/PHPMailerAutoload.php');
require('PHPFiles/class.smtp.php');
require('PHPFiles/class.phpmailer.php');

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;    
$mail->IsSMTP();
$mail->Host = '74.208.5.2';
$mail->SMTPAuth = true;
$mail->Username = 'info@mydomain.com';           
$mail->Password = '*******';                          
$mail->SMTPSecure = 'tls';
$mail->Port = 25; connect to

$mail->From = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->FromName = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->addAddress('info@mydomain.com'); 
$mail->Subject = isset($_POST["subject"]) ? $_POST["subject"] : "";
$mail->Body = str_replace("\n",'<br>', $_POST['message']);

$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->isHTML(true); 

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

At the moment, the Body of the email contains the contents of the textare field. How can the body contain this, but then also a line break, then, the contents of the number field?

Upvotes: 0

Views: 3354

Answers (2)

Akshay
Akshay

Reputation: 2229

You might want to try this. Make a separate variable for all the data that needs to be in the body of the message.

$message = "Name :- " . $_POST['name'] . "<br>" . " Number :- " . $_POST['number'];
$mail->Body = $message;

Upvotes: 3

fsn
fsn

Reputation: 539

so doing it like:

$mail->Body = str_replace('\n','<br />', $_POST['message']);

or:

$mail->Body = str_replace("\\n","<br />", $_POST["message"]);

should fix the problem of string getting the "line breaks".

btw. it`s always good to:

var_dump($data);
die();

to see what are you getting at each step of a code

Upvotes: 0

Related Questions