使用具有适当格式的PHP发送邮件

时间:2015-05-08 06:50:52

标签: php html

我使用php从站点中的注册表单发送邮件。邮件也发送成功。但邮件显示所有的HTML代码。它没有像下面那样显示:

Name : xxxxxx
Age  : 24
Email: example@example.com

我是否知道如何发送上述邮件。我目前使用的编码如下:

<?php
$solvation = "<html>" . $_POST['solvation'];
$name = $_POST['name'];
$to = $_POST['email'];
$msg = $_POST['message'];
$message = '<!DOCTYPE HTML><html>' .
  '<head>' .
  '<meta http-equiv="content-type" content="text/html">' .
  '<title>Email notification</title>' .
  '</head>' .
  '<body>' .
  '<div id="outer" style="width: 80%;margin: 0 auto;margin-top: 10px;">' .
  '<div id="inner" style="width: 78%;margin: 0 auto;background-color: #fff;font-family: Open Sans,Arial,sans-serif;font-size: 13px;font-weight: normal;line-height: 1.4em;color: #444;margin-top: 10px;">' .
  '<p> Solvation        :' . $solvation . '</p>' .
  '<p> Name         :' . $name . '</p>' .
  '<p> Email            :' . $email . '</p>' .
  '<p> Message      :' . $msg . '</p>' .
  '</div>' .
  '</div>' .
  '<div id="footer" style="width: 80%;height: 40px;margin: 0 auto;text-align: center;padding: 10px;font-family: Verdena;background-color: #E2E2E2;">' .
  '</div>' .
  '</body>' .
  '</html>';
//$headers   = $solvation."\n".$name."\n".$email."\n".$message."\n";
$headers = 'From: garunkumar41@gmail.com' . "\r\n" . 'Reply-To: garunkumar41@gmail.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($solvation, $name, $to, $message, $headers);
?>

我想发送上面的电子邮件..我能获得帮助吗?

3 个答案:

答案 0 :(得分:0)

替换

$solvation = $_POST['solvation'];

html

否则你会有两个mail个起始标记,因此HTML无效,导致它像纯文本一样发送。

您的mail($to, $subject, $message, $headers); 函数的参数顺序错误。它应该是

$solvation

我不知道变量$subject包含什么,所以只需用适当的值替换{{1}}。

答案 1 :(得分:0)

试试这个:更改$solvation$message,如下所示

$solvation = $_POST['solvation'];    
 $message   = '<!DOCTYPE HTML><html>
                                <head>
                                <meta http-equiv="content-type" content="text/html">
                                <title>Email notification</title>
                                </head>
                                <body>
                                <div id="outer" style="width: 80%;margin: 0 auto;margin-top: 10px;">
                                <div id="inner" style="width: 78%;margin: 0 auto;background-color: #fff;font-family: Open Sans,Arial,sans-serif;font-size: 13px;font-weight: normal;line-height: 1.4em;color: #444;margin-top: 10px;">
                                <p> Solvation      :'.$solvation.'</p> 
                                <p> Name           :'.$name.'</p>
                                <p> Email          :'.$email.'</p>
                                <p> Message        :'.$msg.'</p>
                                </div>  
                                </div>
                                <div id="footer" style="width: 80%;height: 40px;margin: 0 auto;text-align: center;padding: 10px;font-family: Verdena;background-color: #E2E2E2;">
                               </div>
                                </body> 
                                </html>';

编辑:尝试更改

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";

还要检查您是否在PHP代码中定义了$email

答案 2 :(得分:0)

建议的答案

好的,这是您的固定代码,请注意这是一种发送邮件的坏方法,因为您需要清理输入。换句话说,我的代码可以工作,但您需要使用正则表达式实现清理输入。如果你想知道为什么你的代码不起作用:

1)你有一些错误的终结者;

2)你在var中有错误的值;

3)您的值与代码一起发送;

4)你连接了你的html标记代码(见下文):

'<title>Email notification</title>' .
'</head>'

换句话说,您发送如下:

<title>Email notification</title></head>

这意味着您的html代码将在一行中发送,这对标准的html邮件格式不利,每个标签都应该带回车符,您可以按如下方式编写标记:

$message =
'<title>Email notification</title>
</head>
etc...';

所以你会尊重每个html标签中的自然回车...

供将来参考:http://php.net/manual/en/function.mail.php#example-3785

复制下面的代码并保存为mail.php并试一试

<强> mail.php

<!DOCTYPE html>
<html>
<head>
<meta charset="iso-8859-1">
<title>Email Notification</title>
</head>
<body>
<form action="mail.php" method="post">
Solvation:<br><input type="text" name="solvation" value="<?php echo isset($_POST['solvation']) ? $_POST['solvation']  : ''; ?>"><br><br>
Name:<br><input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name']  : ''; ?>"><br><br>
To:<br><input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email']  : ''; ?>"><br><br>
Message:<br><textarea name="message"><?php echo isset($_POST['message']) ? $_POST['message']  : ''; ?></textarea><br><br>
<input type="submit" name="submit" value="Send"><br><br>
</form>
</body>
</html>
<?php
if (!defined('PHP_EOL')) {
  if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
    define('PHP_EOL',"\r\n");
  } elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
    define('PHP_EOL',"\r");
  } elseif (strtoupper(substr(PHP_OS,0,3) == 'DAR')) {
    define('PHP_EOL',"\n");
  } else {
    define('PHP_EOL',"\n");
  }
}
$solvation = isset($_POST['solvation']) ? $_POST['solvation']  : '';
$name = isset($_POST['name']) ? $_POST['name']  : '';
$to = isset($_POST['email']) ? $_POST['email']  : '';
$msg = isset($_POST['message']) ? $_POST['message']  : '';
$message =
'<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Email notification</title>
</head>
<body>
<div id="outer" style="width: 80%;margin: 0 auto;margin-top: 10px;">
<div id="inner" style="width: 78%;margin: 0 auto;background-color: #fff;font-family: Open Sans,Arial,sans-serif;font-size: 13px;font-weight: normal;line-height: 1.4em;color: #444;margin-top: 10px;">
<p>Solvation: ' . $solvation . '</p>
<p>Name: ' . $name . '</p>
<p>Email: ' . $to . '</p>
<p>Message: ' . $msg . '</p>
</div>
</div>
<div id="footer" style="width: 80%;height: 40px;margin: 0 auto;text-align: center;padding: 10px;font-family: Verdena;background-color: #E2E2E2;">
</div>
</body>
</html>';
// Basilar Settings
$mail_boundary = "=_NextPart_" . md5(uniqid(time()));  // Create a Boundary
$headers = "From: garunkumar41@gmail.com <garunkumar41@gmail.com>" . PHP_EOL;  // From Header
$headers .= "Reply-To: garunkumar41@gmail.com <garunkumar41@gmail.com>" . PHP_EOL;  // Reply-To Header
$headers .= "Return-Path: <garunkumar41@gmail.com>" . PHP_EOL;  // Return-Path Header
$headers .= "MIME-Version: 1.0" . PHP_EOL;  // Mime Version
$headers .= "Content-Type: multipart/alternative;" . PHP_EOL . "  boundary=\"$mail_boundary\"" . PHP_EOL;  // Header MultiPart
$headers .= "X-Mailer: PHP Mailer Script 1.0";  // Name of the Php App
$name = html_entity_decode($name, ENT_QUOTES, 'ISO-8859-1');  // HTML converted in ascii
$solvation = html_entity_decode($solvation, ENT_QUOTES, 'ISO-8859-1');  // HTML converted in ascii
$msgplus = "This is a multi-part message in MIME format." . PHP_EOL . PHP_EOL;  // Multipart Message
// TEXT Message
$msgplus .= "--$mail_boundary" . PHP_EOL;
$msgplus .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . PHP_EOL;
$msgplus .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$msgplus .= "Text Begins Here ->" . strip_tags(html_entity_decode($msg, ENT_QUOTES, 'ISO-8859-1')) . "<- Text Ends Here";
// HTML Message
$msgplus .= PHP_EOL . "--$mail_boundary" . PHP_EOL;
$msgplus .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL;
$msgplus .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$msgplus .= $message;
// Sending Message
$msgplus .= PHP_EOL . "--$mail_boundary--" . PHP_EOL;  // Final Boundary multipart/alternative
@ini_set("sendmail_from", "garunkumar41@gmail.com");  // Used only under Windows
if (isset($_POST['submit'])) {
  $ok = @mail($to, $solvation, $msgplus, $headers, "-fgarunkumar41@gmail.com");
  if ($ok) {
    exit("Mail Sent! Thanks!");
  } else {
    exit("Mail Not Sent! Sorry!");
  }
} else {
  echo "Ready to Send Your Mail";
}
?>