Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2007
    Posts
    24

    Unanswered: How to send Email with attachment using SMTP

    Hi,
    I am using PHP and MySql. To send Email I have following code which
    works fine for me. But I want to send attachment with mail and I am not
    able to do it with my code. can any one know how to send Email with attachment Using Authenticated SMTP Mail Server?

    <?php
    include("Mail.php");
    /* mail setup recipients, subject etc */
    $recipients = "feedback@yourdot.com";
    $headers["From"] = "user@somewhere.com";
    $headers["To"] = "feedback@yourdot.com";
    $headers["Subject"] = "User feedback";
    $mailmsg = "Hello, This is a test.";
    /* SMTP server name, port, user/passwd */
    $smtpinfo["host"] = "smtp.mycorp.com";
    $smtpinfo["port"] = "25";
    $smtpinfo["auth"] = true;
    $smtpinfo["username"] = "smtpusername";
    $smtpinfo["password"] = "smtpPassword";
    /* Create the mail object using the Mail::factory method */
    $mail_object =& Mail::factory("smtp", $smtpinfo);
    /* Ok send mail */
    $mail_object->send($recipients, $headers, $mailmsg);
    ?>

  2. #2
    Join Date
    Apr 2007
    Posts
    24
    I found solution....

    <?php
    require_once "Mail.php"; // PEAR Mail package
    require_once ('Mail/mime.php'); // PEAR Mail_Mime packge

    $from = "Robert Davis <robertdavis@pobox.com>";
    $to = "Sam Hill <sam.hill@aol.com>";
    $subject = 'Test mime message with an attachment';

    $headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);

    $text = 'Text version of email'; // text and html versions of email.
    $html = '<html><body>HTML version of email. <strong>This should be bold</strong></body></html>';

    $file = './sample.txt'; // attachment
    $crlf = "\n";

    $mime = new Mail_mime($crlf);
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html);
    $mime->addAttachment($file, 'text/plain');

    //do not ever try to call these lines in reverse order
    $body = $mime->get();
    $headers = $mime->headers($headers);

    $host = "sasl.smtp.pobox.com";
    $username = "robertdavis@pobox.com";
    $password = "Kdu48Adi3";

    $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
    'username' => $username,'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    }
    else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Please note that Vikrant's solution above relies on using the PEAR repository for PHP.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    there are other solutuions using other classes.. IIRC PHPclasses.net
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Feb 2009
    Posts
    3
    I know I'm probably missing somethig stupid... buuutt trying to figure out why this code isn't attaching an e-mail. it runs... it send e-mail... and below it is the output i get in an e-mail..

    <?php
    require_once "Mail.php";
    require_once('mail\\mime.php');

    // Variable list and what they are for
    // $from - change if using a different email provider than the BHD gmail account
    // $to - the email address to send the reports to
    // $subject - Subject for the email that is sent, change to what the client wants
    // $filepath - path to the file for the e-mail. use double \\ for the directory, surround by double quotes and seperated by commas for multiple files
    // $attach - whether or not to attach the files or to put in the body of the text. Yes attaches, no put sin body
    // $hotst - the SMTP host, change if it is different than gmail
    // $port - Port number for the SMTP server
    // $username - e-mail username login
    // $password - password for e-mail
    // $message - Message for e-mail when using attachments. Leave blank if sending reports in the body
    // $attachtype - is the type of attachment, not neccessarily the extension as Office 2007 has a new extention for Excel. Choices: PDF, XLS, TXT

    $from = "*******@gmail.com";
    $to = "********@comcast.net";
    $subject = 'Test mime message with an attachment';
    $filepath = array('c:\\test.txt');
    $attach = "yes";

    $host = "smtp.gmail.com";
    $port = "587";
    $username = "********@gmail.com";
    $password = "********";
    $body = "Emailed reports";
    $attachtype ="";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'port' => $port,
    'SMTPSecure' => "ssl",
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $html = "<html><body>" . $body . "</body></html>";

    $attachments = new Mail_mime("\n");
    $attachments->setTXTBody($body);
    $attachments->setHTMLBody($html);


    if(strtolower($attach) == "no")
    {
    $body = "";
    for($i = 0; $i <= sizeof($filepath)-1; $i++)
    {
    $body = $body . file_get_contents($filepath[$i]) . "\r\n\r\n";
    }
    }
    else
    {
    for($i = 0; $i <= sizeof($filepath)-1; $i++)
    {
    $attachments->addAttachment($filepath[$i], 'text/plain');
    }

    }
    $body = $attachments->get();

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>



    E-mail i receive has this:
    --=_966b8aad27cda41fd373d33135b5caee
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain; charset="ISO-8859-1"

    Emailed reports
    --=_966b8aad27cda41fd373d33135b5caee
    Content-Transfer-Encoding: quoted-printable
    Content-Type: text/html; charset="ISO-8859-1"

    <html><body>Emailed reports</body></html>
    --=_966b8aad27cda41fd373d33135b5caee--




    soo it's putting the file in teh e-mail.. but not making it an attachmen

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so its sort or working but not quite
    I seem to remember having similar problems years ago, but I cannot remember what I did to fix, I seem to remember it was somethign seemingly trivial and minor

    check your path delimeters.. IIRC PHP prefers the unix style eg c:/windows/path/to/file
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Feb 2009
    Posts
    3
    yeah i remember having this issue years ago too when I was working for another company and I can't remember how I fixed it either... And since I no longer work there.. no longer have access to the coding..

    oh and tried UNIX style paths. still same thing.. thought i had trie it before. but had to make sure.

  8. #8
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Could it be something to do with the encoding of the item in question? i.e. utf-8 vs ascii?

  9. #9
    Join Date
    Feb 2009
    Posts
    3
    Nope.. tried a PDF file with he type as application/pdf is wahat I think I used.. and only showed file information.. pretty much everything i've read maes my coing right.. soooo I know it has to be somethign simple I've overlooked. Possibly in teh settings

Posting Permissions

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