douzhantanju1849 2018-07-26 06:00
浏览 13

附件在php邮件中只收集一个文件而不是多个文件

for upload files;

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

for upload second file

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

For Send Mail

$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
";
$headers.= "Content-Type: text/plain; charset=utf-8
";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " 
boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.

" . "-- 
{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" . 
"Content-Transfer-Encoding: 7bit

" . $message . "

";
$message .= "--{$mime_boundary}
";
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\"};
" . " 
name=\"$files[$x]\"
" ."Content-Disposition: attachment;
" . " filename=\"$files[$x]\"
" .
"Content-Transfer-Encoding: base64

" . $data . "

";
$message .= "--{$mime_boundary}
";}
$success = @mail($to, $subject, $message, $headers);
if ($success) {
$_SESSION['career'] = "Thankyou!! We will contact you soon.";
} else {
$_SESSION['career'] = "Erorr!! Please try again.";}

There is picking only one file the first one from $files array and not sending email also please help me to solve the issue

  • 写回答

1条回答 默认 最新

  • dslh32311 2018-07-26 07:08
    关注

    Using mail() for sending an email is a bad idea. This function isn't always working. Try PHPMailer.

    Sample code :

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

    You can modify the above code according to your needs. You can even attach more than one file.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?