附件仅收集一个文件,而不是php邮件中的多个文件

时间:2018-07-26 06:00:55

标签: php email

用于上传文件;

if($_FILES['photo']['name']!=""){
$target_path_folder = "career/";
$img_name =  time() . rand(11, 99) . basename( $_FILES['photo']['name']);
$target_path = $target_path_folder .$img_name;
move_uploaded_file($_FILES['photo']['tmp_name'], $target_path);}

用于上传第二个文件

if($_FILES['resume']['name']!=""){
$target_path_folderr = "career/";
$resume_name =  time() . rand(11, 99) . basename( $_FILES['resume']['name']);
$target_pathr = $target_path_folderr .$resume_name;
move_uploaded_file($_FILES['resume']['tmp_name'], $target_pathr);}

用于发送邮件

$files = array($img_name, $resume_name);
$to = "info@mydomain.com";
$from = "info@mydomain.com";
$subject ="My subject";
$message = $msg;
$headers = "From: $from";
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: text/plain; charset=utf-8\r\n";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " 
boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "-- 
{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
for($x=0;$x<count($files);$x++){
$filesloc="home/myroot/career/".$files[$x];
$file = fopen($filesloc,"rb");
$data = fread($file,filesize($filesloc));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " 
name=\"$files[$x]\"\n" ."Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";}
$success = @mail($to, $subject, $message, $headers);
if ($success) {
$_SESSION['career'] = "Thankyou!! We will contact you soon.";
} else {
$_SESSION['career'] = "Erorr!! Please try again.";}

仅从$files数组中选择一个文件,并且不发送电子邮件,请帮助我解决问题

1 个答案:

答案 0 :(得分:0)

使用mail()发送电子邮件是一个坏主意。此功能并非始终有效。尝试PHPMailer

示例代码:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);
try {
    $mail->SMTPDebug = 2;               // Enable verbose debug output
    $mail->isSMTP();                    // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;             // Enable SMTP authentication
    $mail->Username = 'user@example.com'; // SMTP username
    $mail->Password = 'secret';      // SMTP password
    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;               // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);       // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

您可以根据需要修改以上代码。您甚至可以附加多个文件。