Monday, August 1, 2011

Sending mail with attachment using PHP & SMTP on Hostgator

I've tested it on Hostgator command line and saved it in b.php. It is sending attachment correctly.





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

$from = "contactusds@downloadformsindia.com";
$to = "rag.fjraggupta1@gmail.com";
$subject = "this is the message from your domain";
$body = "give your message body here";
$host = "mail.downloadformsindia.com";
$username = "contacdtus+downloadformsindia.com";
$password = "passwd";

$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$file="./a.php";
$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);



$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
  echo("
" . $mail->getMessage() . "
");
} else {
  echo("Message Sent successfully");
}
?>

No comments:

Post a Comment