Send attachment file using PHPMailer

杀马特。学长 韩版系。学妹 提交于 2020-01-17 15:05:15

问题


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..?


回答1:


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.




回答2:


<?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>


来源:https://stackoverflow.com/questions/24455076/send-attachment-file-using-phpmailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!