我正在使用wp_mail用我网站上的表格发送邮件。但是当我附加一些文件时,名称类似于"phpr0vAqT“或"phpFO0ZoT”。
$files = array(); //Array pour les fichiers
$count = count(array_filter($_FILES['fichier']['name'])); //Compte le nombre de fichiers
for($i=0;$i<$count;$i++){ //boucle sur chaque fichier
array_push($files, $_FILES['fichier']['tmp_name'][$i]); //insere le fichier dans l'array $files
}
我认为问题来自于:'tmp_name‘,但我不知道我能改变什么,因为wp_mail需要一个路径。
然后,我这样做:
wp_mail($to, $subject, $message, $headers, $files);
发送邮件。
谢谢。
发布于 2018-07-17 07:29:00
上面的方法是正确的,下面是你如何在php / wp中做到这一点的一个例子。希望这能有所帮助!
if(!empty($_FILES['upload-attachment']['tmp_name'])){
//rename the uploaded file
$file_path = dirname($_FILES['upload-attachment']['tmp_name']);
$new_file_uri = $file_path.'/'.$_FILES['upload-attachment']['name'];
$moved = move_uploaded_file($_FILES['upload-attachment']['tmp_name'], $new_file_uri);
$attachment_file = $moved ? $new_file_uri : $_FILES['upload-attachment']['tmp_name'];
$attachments[] = $attachment_file;
}
在完成附件之后,你应该清理干净
unlink($attachment_file);
发布于 2019-09-04 04:49:18
要更改附件名称,应使用phpmailer_init
操作直接访问wp_mail()
中使用的PHPMailer
实例,而不是将$files
作为函数参数传递:
function prefix_phpmailer_init(PHPMailer $phpmailer) {
$count = count($_FILES['fichier']['tmp_name']); //Count the number of files
for ($i = 0; $i < $count; $i++) { //loop on each file
if (empty($_FILES['fichier']['error'][$i]))
$phpmailer->addAttachment($_FILES['fichier']['tmp_name'][$i], $_FILES['fichier']['name'][$i]); //Pass both path and name
}
}
add_action('phpmailer_init', 'prefix_phpmailer_init');
wp_mail($to, $subject, $message, $headers);
remove_action('phpmailer_init', 'prefix_phpmailer_init');
https://stackoverflow.com/questions/50701567
复制相似问题