【发布时间】:2017-04-29 23:52:36
【问题描述】:
我是 php 初学者,我的英语可能是错误的...我试图找到一种方法来通过 phpmailer 发送带有表单附件的邮件。
目前文件上传到服务器上的一个目录,但是有2个文件,一个是好的,另一个是0字节,并且代码附加了那个文件......错误的一个!而且我找不到问题所在...
发送邮件时也没有消息显示...
如果有人可以帮助我,我将非常感激!
HTML:
<form action="formulaire.php" method="post" enctype="multipart/form-data">
<table align="center"><tr><td><label for="nom">Votre nom :</label></td>
<td><input type="text" name="nom" required/><br></td></tr>
<tr><td><label for="prenom">Votre prénom: </label></td>
<td><input type="text" name="prenom" required/><br></td></tr>
<tr><td><label for="societe">Société: </label></td>
<td><input type="text" name="societe" required/><br></td></tr>
<tr><td><label for="phone">Téléphone: </label></td>
<td><input type="text" name="phone" required/><br></td></tr>
<tr><td><label for="email">Votre E-mail: </label></td>
<td><input type="email" name="email" required/><br></td></tr>
<tr><td><label for="message">Texte explicatif :</label></td><br>
<td><textarea name="message" rows="2" cols="50" required></textarea></td></tr>
<tr><td><input type="hidden" name="MAX_FILE_SIZE" value="10000000"> Send this file: <input name="userfile" type="file"></td></tr>
<tr><td></td></tr>
<tr><td align="center"><input type="submit" value="Envoyer"></td></tr></table>
</form>
PHP:
<?php
if (array_key_exists('userfile', $_FILES)) {
$uploadfile = tempnam('upload', $_POST['nom']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile.".jpg")) {
require_once("PHPMailer/class.phpmailer.php");
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->From = $_POST['email'];
$mail->IsMail ();
$mail->ClearAddresses ();
$mail->AddAddress ("xxx@xxxxx.com");
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'xxxxx';
$mail->Body = '<ul>
<li>Nom : '. $_POST['nom'] .'</li>
<li>Prenom : '. $_POST['prenom'] .'</li>
<li>Societe : '. $_POST['societe'] .'</li>
<li>Telephone : '. $_POST['phone'] .'</li>
<li>E-mail : '. $_POST['email'] .'</li>
'.$filename;'
</ul>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->addAttachment($uploadfile, '');
if(!$mail->send()) {
$msg = "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg = "Message sent!";
}
} else {
$msg = 'Failed to move file to ' . $uploadfile;
}
}
?>
【问题讨论】:
-
您定义了
$msg,但从不显示它。检查$uploadfile的值以及对addAttachment()的调用的返回值。不要使用提交者的地址作为发件人地址;它是伪造的,会导致邮件无法通过 SPF 检查。您不需要加载 PHPMailer 类和自动加载器;自动加载器就是您所需要的。 -
您好,感谢您的帮助,我更正了消息现在显示状态!
-
我删除了 phpmailerclass 并保留了自动加载器。我不明白发件人地址的问题!我需要它在我的邮件中......我可以放什么来代替它?我检查了 $uploadfile 的值,它是 0b 文件。所以我改变了 $mail->addAttachment($uploadfile.".jpg", '');然后现在将文件附加在邮件中!应该这样就好了……
-
将您自己的地址放在发件人地址中,将提交者的地址放在使用
addReplyTo()的回复中。
标签: php email phpmailer attachment