Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



using mail function...
 
Share:
Notifications
Clear all

[Solved] using mail function to send multiple attachments in php

2 Posts
2 Users
0 Reactions
1,219 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

Hi need help with my code, its not sending any attachment to any document, i have been struggling for almost a week no with no progress at all. This code does not work 100% because it only send an empty email with no header and attachments.

 

//code to send multiple attachment and email with pdf

<?php
/** Mail with attachment */
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $bcc, $subject, $message){
$uid = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$uid}x";

$header = "gcobani.mkontwana@agilelimitless.org.za: ".$from_name." <".$from_mail.">\r\n";
$header .= "ggcobani@gmail.com: ".$bcc."\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$mime_boundary."\r\n";
$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= nl2br($message)."\r\n\r\n";
$header .= "--".$mime_boundary."\r\n";

foreach($filename as $k=>$v){

$file = $path.$v;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));

$header .= "Content-Type: application/octet-stream; name=\"".$v."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$v."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$mime_boundary."--"."\r\n";
}

$amount =$_POST['amount'];
$purpose =$_POST['purpose'];
$voucher =$_POST['voucher'];
$gender =$_POST['gender'];
$fname =$_POST['fname'];
$lname =$_POST['lname'];
$title =$_POST['title'];
$typeID =$_POST['typeID'];
$email =$_POST['email'];
$number =$_POST['number'];
$maritalstatus =$_POST['maritalstatus'];
$street1 =$_POST['street1'];
$street2 =$_POST['street2'];
$town =$_POST['town'];
$province =$_POST['province'];
$code =$_POST['code'];
$propertyownership =$_POST['propertyownership'];
$placeofwork =$_POST['placeofwork'];
$jobtitle =$_POST['jobtitle'];
$jobstreet=$_POST['jobstreet'];
$jobstreet2 =$_POST['jobstreet2'];
$jobtown =$_POST['jobtown'];
$worknumber =$_POST['worknumber'];
$jobprovince =$_POST['jobprovince'];
$jobcode =$_POST['jobcode'];
$income =$_POST['income'];
$bankname=$_POST['bankname'];
$branchname =$_POST['branchname'];
$accountno =$_POST['accountno'];
$accounttype =$_POST['accounttype'];
$signature =$_POST['signature'];
$bankstatement =$_POST['bankstatement'];
$payslip=$_POST['payslip'];


$send = mail($to, $subject, $body, $headers);



// Connect to database

$conn = new mysqli('localhost','acifinan_acifinan','Finance_01','acifinan_acifinanc');
if ($conn->connect_error)
{die('Connection Failed:'.$conn-> connect_error); }
else
{
$stmt= $conn->prepare("insert into application(amount, purpose, voucher, gender, fname, lname, title, typeID, email, number, maritalstatus, street1, street2, town, province, code, propertyownership, placeofwork, jobtitle, jobstreet, jobstreet2, jobtown, worknumber, jobprovince, jobcode, income, bankname, branchname, accountno, accounttype, signature, id, bankstatement, payslip) values (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)");
$stmt-> bind_param("ssssssssssssssssssssssssssssssssss" , $amount,$purpose,$voucher,$gender,$fname,$lname,$title,$typeID,$email,$number,$maritalstatus,$street1,$street2,$town,$province,$code,$propertyownership,$placeofwork,$jobtitle,$jobstreet,$jobstreet2,$jobtown,$worknumber,$jobprovince,$jobcode,$income,$bankname,$branchname,$accountno,$accounttype,$signature,$id,$bankstatement,$payslip);
$stmt->execute();
echo "Submitted Successfully";
$stmt->close();
$conn->close();

}
}
?>


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2741
 

Apologies for the late approval.

Unfortunately, I'm not an expert when it comes to PHPMailer.
Did you check out the examples? Like this one (assuming this is the PHPMailer you've been using)?

<?php

/**
 * PHPMailer multiple files upload and send example
 */

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;

require '../vendor/autoload.php';

$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    //Create a message
    $mail = new PHPMailer();
    $mail->setFrom('from@example.com', 'First Last');
    $mail->addAddress('whoto@example.com', 'John Doe');
    $mail->Subject = 'PHPMailer file sender';
    $mail->Body = 'My message body';
    //Attach multiple files one by one
    for ($ct = 0, $ctMax = count($_FILES['userfile']['tmp_name']); $ct < $ctMax; $ct++) {
        //Extract an extension from the provided filename
        $ext = PHPMailer::mb_pathinfo($_FILES['userfile']['name'][$ct], PATHINFO_EXTENSION);
        //Define a safe location to move the uploaded file to, preserving the extension
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct])) . '.' . $ext;
        $filename = $_FILES['userfile']['name'][$ct];
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            if (!$mail->addAttachment($uploadfile, $filename)) {
                $msg .= 'Failed to attach file ' . $filename;
            }
        } else {
            $msg .= 'Failed to move file to ' . $uploadfile;
        }
    }
    if (!$mail->send()) {
        $msg .= 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        $msg .= 'Message sent!';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHPMailer Upload</title>
</head>
<body>
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000">
        Select one or more files:
        <input name="userfile[]" type="file" multiple="multiple">
        <input type="submit" value="Send Files">
    </form>
<?php } else {
    echo htmlspecialchars($msg);
} ?>
</body>
</html>

 


   
ReplyQuote
Share: