[英]Attachment not sent with Email
I am developing a small E-mail app using PHP, For sending out emails I use PHPMailer, All the functionalities work apart form one "Attachments". 我正在使用PHP开发一个小型的电子邮件应用程序,要使用PHPMailer发送电子邮件,所有功能都可以通过一个“附件”分开工作。 Attachments just don't go through with the sent email.
附件只是不与发送的电子邮件一起处理。 I tried many different ways to fix it but the one I have now I belief is not far off a working order....
我尝试了多种方法来修复它,但是我现在相信的那个方法离工作秩序并不遥远。
I will post small parts of code that deal with the Attachment process.... 我将发布处理附件过程的一小部分代码。
HTML Code: HTML代码:
<form action="php/smtp_saved.php" method="post">
<input type="file" name="attach" id="attach" />
</form>
PHP Code: PHP代码:
if ($_POST['action'] == 'Send') {
if (preg_match('<,>', $email['recipient'])) {
$address = explode(',', $email['recipient']);
if (sizeof($address) < 19) {
foreach ($address as $recipient) {
$save = new saveSaved();
$save->save($_SESSION['user'], $email['recipient'], gmdate('Y-m-d H:i:s', strtotime ('+1 hour')), $_SESSION['delete'],$email['HTML']);
require_once('../PHPMailer/PHPMailerAutoload.php');
require_once('../PHPMailer/class.smtp.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = '465';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $_SESSION['user']; // SMTP username
$mail->Password = $_SESSION['pass']; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->From = $_SESSION['user'];
$mail->FromName = 'OneTwoTrade';
//$mail->addAddress($email['recipient']); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addAddress(trim($recipient));
if (preg_match('<,>', $email['cc'])) {
$cc = explode(',', $email['cc']);
if (sizeof($cc) < 9) {
foreach ($cc as $carbonC) {
$mail->addCC($carbonC);
}
} else {
exit ('Max 10 Recipients Per Email');
}
} else {
$mail->addCC($email['cc']);
}
//$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
$file = $_FILES['attach']['name'];
$mail->AddAttachment($file);
}
/*
if(isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK){
$mail->addAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
*/
// Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email['subject'];
$mail->Body = $email['HTML'];
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->SMTPDebug =1;
if (!$mail->send()) {
header("Location: ../saved_emails.php?error2");
//echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
}
}
}
Can someone help me to spot a mistake or direct to a decent source of information....? 有人可以帮助我发现错误或直接获得体面的信息来源吗?
When attaching a file via a form add enctype="multipart/form-data"
attribute to the form
tag. 通过表单附加文件时,将
enctype="multipart/form-data"
属性添加到form
标签。 I think the problem occurs because of this. 我认为是因为这个问题。
<form action="php/smtp_saved.php" method="post" enctype="multipart/form-data">
<input type="file" name="attach" id="attach" />
</form>
After a little search, I have found this: Both temp_name
and name
keys for the attached file are used in AddAttachment()
function: 稍作搜索后,我发现了这一点:
AddAttachment()
函数中同时使用了附件的temp_name
和name
键:
if(isset($_FILES['attach']) && $_FILES['attach']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);
}
$file = $_FILES['attach']['name'];
should be $file = $_FILES['attach']['tmp_name'];
应该是
$file = $_FILES['attach']['tmp_name'];
. 。 The actual file data is in
$_FILES['attach']['tmp_name']
. 实际的文件数据在
$_FILES['attach']['tmp_name']
。 $_FILES['attach']['name']
is just the name given to the uploaded file on the client side. $_FILES['attach']['name']
只是客户端上载文件的名称。
OK guys Want to thx all of you who tried to help me and pushed me to the solution, I managed to fix it this is what I had to do... 好的伙计们想感谢所有试图帮助我并将其推向解决方案的人,我设法解决了这个问题,这是我必须要做的...
if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
$file = $_FILES['attach']['tmp_name'];
$name = $_FILES['attach']['name'];
$mail->AddAttachment($file, $name);
}
:))))))))))))))))))))))))))))))))))))))))) :)))))))))))))))))))))))))))))))))))))))))
Works like a charm, just checked the PHPMailer Documentation and the AddAttachment Method needs two parameters, 就像超级按钮一样工作,只需检查PHPMailer文档和AddAttachment方法需要两个参数,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.