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
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.