如何在php中发送包含换行符的电子邮件

时间:2018-06-09 07:09:04

标签: php email

请帮我在换行符中创建代码消息。我不知道怎么做,请检查我的代码。希望你帮帮我。非常感谢: -

<?php 
if(isset($_POST['submit'])){
$to = "mymail@gmail.com"; // this is your Email address
$from = $_POST['name']; // this is the sender's Email address
$first_name = $_POST['name'];
$mail=$_POST['email'];
$msg=$_POST['message'];

$phone= $_POST['mob'];


$subject = "contact";
$subject2 = "Copy of your form submission";
$message =  "Name:-". $first_name . "Email:-". $mail . "Phone Number:- 
   ".$phone. " Message:-".$msg." " ;

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);

 ?>

我想收到这样的信息: -

   Name:-loreal
   email:- Mymail
   phone Number:- 987654321
   Message:- Hi 

4 个答案:

答案 0 :(得分:1)

当你在电子邮件模板中使用html标签时,你必须在标题中提到内容类型,你必须在邮件功能中包含这些标题

$ headers ='MIME-Version:1.0'。 “\ r \ n”; $ headers。='Content-type:text / html; charset = iso-8859-1'。为 “\ r \ n” 个;

邮件($到,$主题,$消息,$头);

答案 1 :(得分:0)

尝试在邮件中使用\n

$message =  " Name:-". $first_name . "\n Email:-". $mail . "\n Phone Number:- 
   ".$phone. "\n Message:-".$msg." " ;

他们是逃脱序列。 \n是换行符,\r是回车符。 \r是回车\n是换行(或换行)

  • Mac:\r
  • Linux / Unix:\n
  • Windows:\r\n

答案 2 :(得分:0)

对于HTML邮件,请使用正确的标题:

<?php 
    if(isset($_POST['submit'])){
        $to = "mymail@gmail.com"; // this is your Email address
        $from = $_POST['name']; // this is the sender's Email address
        $first_name = $_POST['name'];
        $mail=$_POST['email'];
        $msg=$_POST['message'];
        $phone= $_POST['mob'];
        $subject = "contact";
        $message = "Name:-". $first_name . "<br/>Email:-". $mail . "<br/>Phone Number:-".$phone. "<br/>Message:-".$msg." " ;
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        mail($to,$subject,$message,$headers);
 ?>

答案 3 :(得分:0)

<?php 
if(isset($_POST['submit'])){
$to = "mymail@gmail.com"; // this is your Email address
$from = $_POST['name']; // this is the sender's Email address
$first_name = $_POST['name'];
$mail = $_POST['email'];
$msg = $_POST['message'];

$phone= $_POST['mob'];

$subject = "contact";
$subject2 = "Copy of your form submission";
$message =  "Name:-" . $first_name . "\n\n" . "Email:-" . $mail . "\n\n" . "Phone Number:- " .$phone . "\n\n" . " Message:- " . $msg;

$headers = "From:" . $from;
$headers .= " MIME-Version: 1.0\r\n"; #Define MIME Version
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; #Set content type

mail($to,$subject,$message,$headers);

header('Location: thank-you.php');
    }
    else{
echo 'Your message cannot be sent.';
}

?>