我一整天都在搜索,但没有找到有效的代码。我在提交表格后正在尝试
这是缩小版
<?php
$to = "email@email.com";
if(isset($_POST['submit']))
{
// VALIDATION
if(empty($_POST['address']))
{
"First Name Required";
}
if(empty($_POST['email']))
{
"Last Name Required";
}
if(empty($error))
{
$subject = 'The Form';
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: text/html; \r\n" ;
$headers .= "From: from@theemailaddress.com\r\n"."Reply-to: {$_POST['email']}\r\n";
$msg .="<html>
<head></head>
<body>
<table>
<tr><td>
<table>
<tr><td>This is the email sent.</td></tr>
</table>
</body>
</html>";
include('con.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thetable");
$email= mysqli_real_escape_string($con, $_POST['email']);
$address= mysqli_real_escape_string($con, $_POST['address']);
$sql = "SELECT * FROM thetable WHERE `email` = '{$email}' OR `address` = '{$address}'";
$result = mysqli_query($con,$sql);
if(($result->num_rows)>= 1)
{
$theerror = "You exist";
}
else
{
$sql="INSERT INTO thetable(email, address) VALUES ('$_POST[email]','$_POST[address]'";
$success = "Sent ... Insert it!!!";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
//The Attachment
$cr = "\n";
$data = "Email" . ',' . "address" . ',' . $cr;
$data .= "$email" . ',' . "$address" . $cr;
$fp = fopen('diploma_apprenticeship_form_sub.csv','a');
fwrite($fp,$data);
fclose($fp);
$attachments[] = Array(
'data' => $data,
'name' => 'diploma_apprenticeship_form_sub.csv',
'type' => 'application/vnd.ms-excel'
);
//Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Add the headers for a file attachment
$headers = "MIME-Version: 1.0\n" .
"From: {$from}\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Add a multipart boundary above the plain message
$msg= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$text . "\n\n";
//Add sttachments
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$msg.= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ;
}
$msg.= "--{$mime_boundary}--\n";
$result = @mail($to, $subject, $msg, $headers);
}
mysqli_close($con);
{
}
}
}
?>
答案 0 :(得分:1)
试试这个,
$f = fopen('path to file', 'w'); //path such as __DIR__./file.csv'; write mode.
fputcsv( $f, $data);
//data is an array of data ie array('one', 'two'); is one,two in the file ~ make a loop
//around this and write as many lines as you need, like array(header, header1); then
//array(data, data1) etc...
fclose($f); //close the file when done
http://phpmailer.worxware.com/
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->From = 'from@example.com';
$mail->addAddress('joe@example.net', 'Joe User');
$mail->addAttachment( __DIR__.'/file.csv');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
unlink( __DIR__.'/file.csv' ); //remove the file
哦,摆脱所有那些标题的东西,让php邮件程序完成它的工作,正如你所提到的那样,不是为了验证你的数据。制定流程。
输入 验证 计算(赋值) 输出(发送电子邮件) 清理(删除任何文件等)。
等。
AS更新
$msg .="<html>
<head></head>
<body>
<table>
<tr><td>
<table>
<tr><td>This is the email sent.</td></tr>
</table>
</body>
</html>";
......然后是后者
$msg= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$text . "\n\n";
此外,您可能会收到第一条消息的警告,因为您在使用concant之前未定义变量。
$msg .= 'something';
应该是
$msg = 'something';
或者
$msg = '';
$msg .= 'something';