phpmailer将更多内容添加到电子邮件正文中

时间:2015-07-24 16:40:24

标签: php email phpmailer

我是phpmailer的新手。我通过创建一个允许用户向我们的收件箱发送电子邮件的简单表单取得了一些进展:

<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';
}

目前,电子邮件的正文包含textare字段的内容。身体怎么能包含这个,然后还有一个换行符,然后是数字字段的内容?

2 个答案:

答案 0 :(得分:2)

您可能想尝试一下。为需要在消息正文中的所有数据创建一个单独的变量。

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

答案 1 :(得分:0)

这样做:

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

或:

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

应该解决字符串获取&#34;换行符的问题&#34;。

顺便说一句。它始终是好的:

var_dump($data);
die();

了解您在代码的每一步获得了什么