SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File attachment and Phpmailer class

    I would like to send email with file attached using Html form and phpmailer class. While I am able to send the email properly, i am not able to attach the file with the message. Could anyone who has used phpmailer show me how a file input from a html form can send the message with the file as attachment.

    Below is my mailform.php:

    <form action="index.php?action=sendmail" method="post" enctype="multipart/form-data" name="theform">
    <input type="hidden" name="op" value="send_email" />
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
    <tr>
    <td width="13%">From/td>
    <td colspan="2"><input name="fromemail" type="text" id="fromemail" size="45" value="<?php echo stripslashes($from) ?>" ></td>
    </tr>
    <tr>
    <td>To/td>
    <td colspan="2"><input name="toemail" type="text" id="toemail" value="<?php echo $to ?>" ></td>
    </tr>
    <tr>
    <td>Subject</td>
    <td colspan="2"><input name="subject_field" type="text" id="subject_field" size="45" value="<?php echo stripslashes($subject) ?>" ></td>
    </tr>
    <tr>
    <td>Body</td>
    <td colspan="2"><textarea name="textmsg" cols="80" rows="10" value="<?php echo stripslashes($mailbody) ?>"></textarea></td>
    </tr>
    <tr>
    <td>File Attachment </td>
    <td colspan="2"><input name="file_attach" type="file" id="file_attach"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td width="17%"><input name="Sendmail" type="submit" id="Sendmail" value="Send Mail"> </td>
    <td width="70%"><input name="Cancel" type="button" id="Cancel" value="Cancel"></td>
    </tr>
    </table>

    </form>

    Now how must I use the phpmailer class to send attachments?

    Thanks for the help.

  2. #2
    SitePoint Member
    Join Date
    May 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Split it into two operations. First handle the upload (documented thoroughly here: http://www.php.net/features.file-upload), then attach the uploaded file to the message. Attaching a file is covered very clearly in the phpmailer docs: http://phpmailer.sourceforge.net/tutorial.html#3

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have read through the links provided, but I seem to be missing something. Could you pls tell me what is wrong in the following code:

    function sendemail() {
    // instantiate the class
    $mailer = new FreakMailer();
    $mailer->From = $_POST["fromemail"];
    $mailer->To = $_POST["toemail"];
    $mailer->Subject = $_POST["subject_field"];
    $mailer->Body = $_POST["textmsg"];
    $mailer->WordWrap = 50;

    //for file attachments

    $uploaddir = 'uploads/';
    $uploadfile = $uploaddir . basename($_FILES['file_attach']['name']);
    move_uploaded_file($_FILES['file_attach']['tmp_name'], $uploadfile);
    $firstfile = $_POST["file_attach"];
    $mailer -> Addattachment($firstfile, testpdf, "base64", "application/pdf");


    if(!$mailer->Send())
    {
    echo 'There was a problem sending this mail!';
    }
    else
    {
    echo 'Mail sent!';
    }
    $mailer->ClearAddresses();
    $mailer->ClearAttachments();

    }

    I'm not very good at php, so would appreciate a detail explaination.

    Thanks for all the help.

  4. #4
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try:

    PHP Code:
     //for file attachments

    $uploaddir 'uploads/';
    $uploadfile $uploaddir basename($_FILES['file_attach']['name']);
    move_uploaded_file($_FILES['file_attach']['tmp_name'], $uploadfile);
    $mailer -> Addattachment($uploadfiletestpdf"base64""application/pdf"); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be replying after a long time. Have been busy lately. Thanks for the suggestion, however, the change doesn't seem to work.The email with the body and subject is sent but not the attachment. Why is that so? Does anyone have a working example of the phpmailer class using attachment? Would appreciate all the help.

    Thanks

  6. #6
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works for me:
    I'm using SMTP so if you don't want that, just delete those top 2 lines

    PHP Code:
    // instatiate mailer class
    $mail = new PHPMailer();
            
    // mail settings
    $mail->IsSMTP();
    $mail->Host 'localhost';
    $mail->From 'mailer@domain.com';
    $mail->FromName "AutoMailer";
    $mail->IsHTML(true);
    $mail->Subject 'Subject';
    $mail->Body 'Body';
    $mail->AddAddress('address_here');
    $mail->AddAttachment('path_to_uploaded_file''filename.whatever');
    $mail->send(); 

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. However, I was looking for an example where the user attaches a file with an email form (as shown in my 1st post) and the email with the file attached is sent to the receiver. My previous script works fine without the file attached. I am not sure why the file doesn't upload to the upload folder. Any ideas?

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •