Showing posts with label smtp. Show all posts
Showing posts with label smtp. Show all posts

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");
}
?>

Sending email using PHP/SMTP on Hostgator command line


If you want to send email from Hostgator command line there here is the working sample.
Just replace from email id, to email id, host,username and password.

<?php
require_once "Mail.php";

$from = "contactus@mysite.com";
$to = "to email id";
$subject = "this is the message from your domain";
$body = "give your message body here";
$host = "mail.mysite.com";
$username = "contactus+mysite.com";
$password = "pass123";
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$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"); }
?>