[英]PHP mail function send mail without attachment
我正在嘗試使用 PHP 中的郵件 function 發送帶附件的郵件,我的問題是有時郵件成功到達附件,有時附件不包含在郵件中,發送的郵件沒有附件。 當我檢查 tmp 文件夾時,我找不到該文件。
我的 php 代碼:
<?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 = 'mh6@gmail.com';
// 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>
請嘗試使用 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}";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.