Reputation: 1
I am trying to send mail with attachment using mail function in PHP, my problem is that sometimes the mail arrives successfully with the attachment and other times the attachment is not included in the mail! The mail is sent with no attachment, and when I check the tmp folder then I do not find the file.
my php code:
<?php
if(isset( $_POST['submit'] ))
{
$First = $_POST['First'];
$Last = $_POST['Last'];
$Email = $_POST['email'];
$phone = $_POST['phone'];
$position = $_POST['position'];
$experience = $_POST['experience'];
$Address = $_POST['address'];
$Full_Name = $First."".$Last;
$attachment=$_FILES["attachment"]["tmp_name"];
$folder="tmp/";
$file_name=$_FILES["attachment"]["name"];
$to = '[email protected]';
// Sender
$from = "$Email";
$fromName = "$Full_Name";
// Email subject
$subject = 'Mail From Applied Candidate';
// Attachment file
$file = $folder."".$file_name;
// Email body content
$htmlContent = '
<h3>Candidate Information</h3>
<p>Name: '.$Full_Name.'</p>'.'<p>Email: '.$Email.'</p>'.'<p>Phone: '.$phone.'</p>'.'<p> Address: '.$Address.'</p>'.'<p>Position: '.$position.'</p>'.'<p>Experience: '.$experience.'</p>';
// Header for sender info
$headers = "From: $fromName"." <".$from.">";
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
if(!empty($file) > 0){
if(is_file($file)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file,"rb");
$data = @fread($fp,filesize($file));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
// Send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);
}
?>
html:
<form enctype="multipart/form-data" id="survey-form" action = "applyData" method="POST">
<!-- name -->
<label id="name-label" class="row-label" for="name">First Name:</label>
<input name="First" class="row-input" type="text" placeholder="Enter your First name" required>
<label id="name-label" class="row-label" for="name">Last Name:</label>
<input name="Last" class="row-input" type="text" placeholder="Enter your Last name" required>
<!-- email -->
<label id="email-label" class="row-label" for="email">Email:</label>
<input name="email" class="row-input" type="email" placeholder="Enter your email" required>
<!-- address -->
<label id="email-label" class="row-label" for="email">Address:</label>
<input name="address" class="row-input" type="text" placeholder="Enter your address" required>
<!-- Phone -->
<label id="number-label" class="row-label" for="number">Phone Number:</label>
<input name="phone" class="row-input" type="number" placeholder="Enter your phone number" required>
<label id="name-label" class="row-label" for="name">Position you are applying for:</label>
<input name="position" class="row-input" type="text" placeholder="Enter Position" required>
<!-- years of experience -->
<label id="number-label" class="row-label" for="number">Years of experience</label>
<input name="experience" class="row-input" type="number" placeholder="Enter number of years of experience" required>
<label id="name-label" class="row-label" for="name">Upload Cv:</label>
<input name="attachment" accept=".pdf,.doc,.docx" class="row-input" type="file" required>
<!-- submit button -->
<button id="submit" value="submit" type="submit" name = "submit">Submit</button>
</form>
Upvotes: 0
Views: 192
Reputation: 371
Please try using PHP Mailer:
https://github.com/PHPMailer/PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $_SERVER["DOCUMENT_ROOT"].'/PHPMailer/src/Exception.php';
require $_SERVER["DOCUMENT_ROOT"].'/PHPMailer/src/PHPMailer.php';
$upload_dir = "/path_to_uploads_folder/";
if($_REQUEST['attachment']){
$file_arr = explode(',',$_REQUEST['attachment']);
$p = count(file_arr);
for ($q=0; $q <$p ; $q++) {
$files[] = $upload_dir.$file_arr[$q];
}
}
$mail1 = new PHPMailer(true);
try {
$mail1->setFrom($email);
$mail1->addAddress($to);
for($i=0;$i<count($files);$i++)
{
$file = $files[$i];
$mail1->addAttachment($file);
}
$mail1->isHTML(true);
$mail1->Subject = $subject;
$mail1->Body = $body;
$mail1->AltBody = $body;
$mail1->send();
} catch (Exception $e) {
?>
<script language="javascript">
var message = '<?php echo "Message could not be sent. Mailer Error: {$mail1->ErrorInfo}" ?>';
alert(message);
window.location='https://www.example.com/';
</script>
<?php
exit;
echo "Message could not be sent. Mailer Error: {$mail1->ErrorInfo}";
}
Upvotes: 1