[英]How to modifiy the code for send a message without attachement?
i need to send a message on my script but without an attachement because when i don't attach a file, i get this error : "Error : your file must be type :3MF/STL/OBJ/PLY/STP/SVG/DXF/IGS/PDF/JPEG/PNG ou DOC." 我需要在我的脚本上发送一条消息,但没有附件,因为当我没有附加文件时,我收到此错误:“错误:您的文件必须是类型:3MF / STL / OBJ / PLY / STP / SVG / DXF DOC / IGS / PDF / JPEG / PNG。“
I have tried some example on internet but i'm stucked here. 我在互联网上尝试了一些例子,但我被困在这里。
<?php
$errormsg = "";
if (empty($_POST["fname"])) {
$errormsg .= "Name required. ";
} else {
$fname = $_POST["fname"];
}
if (empty($_POST["email"])) {
$errormsg .= "Email required. ";
} else {
$email = $_POST["email"];
}
/*if (empty($_POST["phone"])) {
$errormsg .= "Phone required. ";
} else {
$phone = $_POST["phone"];
}*/
$phone = $_POST["phone"];
if (empty($_POST["service"])) {
@$service .= "";
} else {
$service = $_POST["service"];
}
if (empty($_POST["message"])) {
$errormsg .= "Message required. ";
} else {
$message = $_POST["message"];
}
if (array_key_exists('userfile', $_FILES)) {
if ($_FILES['userfile']['size'] > 104857666) {
$errormsg .= "Votre fichier ne dois pas faire plus de 100 MO. ";
}
$allowed = array('3mf','stl' ,'OBJ','PLY','STP','svg','dxf','igs','pdf','jpeg','png','doc');
$filename = $_FILES['userfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
$errormsg .= "Votre fichier doit être de type 3MF/STL/OBJ/PLY/STP/SVG/DXF/IGS/PDF/JPEG/PNG ou DOC. ";
}
}
$success = '';
if (!$errormsg){
require_once "mgs-functions.php";
//email subject (Change here)
if($subject)
$mail->Subject = $subject;
else
$mail->Subject = "Nouvelle demande de devis !";
$mgsetemple = false; //Boolean true/false true: email template false: plain text email
$body_message = "";
if(!$mgsetemple) {
//prepare email body [for Plain email use this]
$body_message .= "Adresse IP du client: " . get_client_ip() ."<br>";
$body_message .= "Nom et prénom du client: " . $fname ."<br>";
$body_message .= "Adresse e-mail du client: " . $email ."<br>";
$body_message .= "Numéro de téléphone du client: " . $phone ."<br>";
$body_message .= "Service voulu: " . $service ."<br>";
$body_message .= "\n\n". $message;
}
else{
//prepare email body [Using email template]
$body_message = file_get_contents('mgsc-email-template/mgsc-email-template.php');
$mgsemailshorttag = array("[mgs-sender-ip]", "[mgs-sender-name]", "[mgs-sender-email]", "[mgs-sender-phone]", "[mgs-sender-service]", "[mgs-sender-message]");
$mgsemailshorttagvalue = array(get_client_ip(), $fname, $email, $phone, $service, $message);
$body_message = str_replace($mgsemailshorttag, $mgsemailshorttagvalue, $body_message);
}
$mail->Body = $body_message;
if (array_key_exists('userfile', $_FILES)) {
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
$sname = strtolower(str_replace(" ", "-", $fname));
$uploadfile = 'uploads/' . substr( base_convert( time(), 10, 36 ) . md5( microtime() ), 0, 8 ). '-' . $sname . '.' . $ext;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Attach the uploaded file
$mail->addAttachment($uploadfile, $sname .'file-'. $_FILES['userfile']['name']);
}
}
//send mail
if(!$mail->send())
{
//delete files from server
if (file_exists($uploadfile)){
unlink($uploadfile);
}
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
//delete files from server
if (file_exists($uploadfile)){
unlink($uploadfile);
}
echo "Votre fichier est parvenu jusqu'à nos techniciens, merci !";
}
}
else {
echo "Erreur: ".$errormsg;
}
?> ?>
I have tried some tips but don't work yet 我尝试了一些提示,但还没有工作
My form processor is a all-config-in-form type generic processor. 我的表单处理器是一个全配置形式的通用处理器。 For handling attachments, while I don't discriminate against file types I do have to generate different headers, etc. for sending a plain message vs. message w/ file(s) attached. 为了处理附件,虽然我不区分文件类型,但我必须生成不同的标题等,用于发送普通消息与附加文件的消息。 To detect them, I check to see if there is a $_FILES element that has an error code of 0 (no errors), if so add that element to a new array, and then later process that new array when actually adding the attachments. 为了检测它们,我检查是否有一个$ _FILES元素,其错误代码为0(无错误),如果是,则将该元素添加到新数组,然后在实际添加附件时处理该新数组。
Here's how I do it... 我是这样做的......
$hasFile=false;
foreach($_FILES as $k=>$v){
if($v['error']===0){
$hasFile=true;
$attachments[]=$v;
}
}
Then later, 然后,
if($hasFile){
// build headers for sending w/ attachment and process attachments to add
}else{
// build headers for regular message
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.