-1

Here my code that using PHPMailer to send attachment via mail using PHPMailer. I try this out in localhost it function perfectly. But once I upload to my server, the attachment is not sent.

$email = new PHPMailer();
$email->From      = 'john@hotmail.com';
$email->FromName  = 'John';
$email->Subject   = 'Message Subject';
$email->Body      = 'test';
$email->AddAddress( 'william@hotmail.com' );

$file_to_attach = $_FILES["cv"]["tmp_name"];

$email->AddAttachment( $file_to_attach , $_FILES["cv"]["name"]);

return $email->Send();

I cant receive my attachment file and I upload.

Did I miss out any code..?

rguarascia.ts
  • 694
  • 7
  • 19
user3663143
  • 63
  • 2
  • 9
  • What is your reasoning for using phpmailer? There is a method of emailing w/ attachments which is purely PHP. – rguarascia.ts Jun 27 '14 at 15:17
  • possible duplicate of [Send File Attachment from Form Using phpMailer and PHP](http://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php) – Gunaseelan Jun 27 '14 at 15:18
  • @Rynoh97 because PHPMailer get work faster without write a lot of code. correct..? – user3663143 Jun 27 '14 at 15:18
  • @user3663143 it does... but I suggest the PHP method. I am currently trying to fix your problem, but I **suggest** this method: http://webcheatsheet.com/php/send_email_text_html_attachment.php but that is just me. – rguarascia.ts Jun 27 '14 at 15:20
  • @Rynoh97 thanks you reply. I tried my code execute in localhost is send me the body content with attachment. But when I upload to my server, the attachment file is not show – user3663143 Jun 27 '14 at 15:22
  • @user3663143 it my be because you do not have permission to access your server because you do not log in (as far as I can see with your code) – rguarascia.ts Jun 27 '14 at 15:23
  • @Rynoh97 what does your mean (I do not log in with access permission) ? – user3663143 Jun 27 '14 at 15:24
  • @user3663143 Well if the file is on a server, you might need to be logged into the server to access the file. Is it on a FTP server or a home server. I can determine by your response if that is the issue. – rguarascia.ts Jun 27 '14 at 15:26
  • @Rynoh97 yes..is in my FTP server. What should I do for solving my problem? – user3663143 Jun 27 '14 at 15:26
  • @user3663143 you can first, `ftp_get` the file, store in somewhere then send the file from wherever you saved it. For reference to `ftp_get` : http://www.php.net/manual/en/function.ftp-get.php . – rguarascia.ts Jun 27 '14 at 15:48
  • I wouldn't use that code. It's full of holes, open to header injection attack, won't handle encodings right. You can pretty much guarantee that if you call `mail()` yourself, you're doing it wrong, not least because most example code you find is wrong too. Use a library. – Synchro Jun 27 '14 at 15:50
  • @Rynoh97 I had tried using mail() from reference this website http://www.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-using-php/ But the attachment file output is a bundle of code. – user3663143 Jun 27 '14 at 16:14

2 Answers2

0

There's nothing wrong with your code - it works fine for you on localhost (and also for me), so it must be an issue with your server environment. This could be several things:

Windows server with no mail server installed? It could be permissions for your uploaded files, or indeed the ability to upload files at all, since that is also controlled via php.ini.

I suggest you separate out the stages:

  • Upload a file and save it, check that it's there on the server.

  • Check that your PHP script can see and read the uploaded file (and please read the docs on how to do this correctly).

  • Send a simple email without attachments. If that works you know your mail server is ok.

When all the individual pieces are working, join them together.

Synchro
  • 35,538
  • 15
  • 81
  • 104
0
<?php 
if(isset($_POST['sbt_resume'])){
    $ext = explode('.',$_FILES['upload_resume']['name']);
    $extension = $ext[1];
    $newname = uniqid();
    $full_local_path = 'resume/'.$newname.'.'.$extension;
    $upld = move_uploaded_file($_FILES['upload_resume']['tmp_name'], "$full_local_path");
    require_once('PHPMailer/class.phpmailer.php');
    $emailid = new PHPMailer();
    $emailid->IsSMTP(); // enable SMTP
    $emailid->SMTPAuth = true; // authentication enabled
    $emailid->Host = "smtp.gmail.com";
    $emailid->Port = 465; // or 587
    $emailid->IsHTML(true);
    $emailid->SMTPSecure = 'ssl';
    $emailid->Username = "yourgmail@gmail.com";
    $emailid->Password = "";
    $emailid->From = "yourgmail@gmail.com";
    $emailid->FromName = $name;
    $emailid->Subject = ".";
    $emailid->Body = "Atachement";
    $emailid->AddAddress("");
    $emailid->AddBCC($email);
    $emailid->AddAttachment($full_local_path);
    $emailid->Send();
    echo "<font style='color: green; margin-top: 10px;'>Thank you for upload your Resume we will get back you soon.</font>";
}
?>

<form class="contact-form clearfix" action="" method="post" enctype="multipart/form-data">
 <div class="row">      
        <!-- col-md-3 -->
        <div class="col-md-4 col-sm-4 col-xs-4">
            <div class="input-label">
                <p style="padding-top: 10px;">upload <span>*</span></p>
            </div>
        </div>
        <div class="col-md-4 col-sm-4 col-xs-4">
                <input accept="" type="file" name="upload_resume" class="valid" style="border: 1px solid #E0E0E0;padding: 12px 15px;">
        </div>
        <!-- col-md-9 -->
    </div>
    <!-- row --> 

    <!-- row -->
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <p class="contact-button clearfix">                    
                <span><input type="submit" name="sbt_resume" value="Send" id="submit-contact"></span>
            </p>
        </div>
        <!-- col-md-12 -->
    </div>
    <!-- row --> 
</form>
Pankaj Upadhyay
  • 2,114
  • 21
  • 22