PHP - send emails through SMTP in PHP - Asked By chandana on 31-Jan-12 11:36 PM

Hi..
i need to send emails using PHP with linux environment. pls help me...
Anoop S replied to chandana on 31-Jan-12 11:45 PM
Try this

<?php
 require_once "Mail.php";
 
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 
 $host = "mail.example.com";
 $username = "smtp_username";
 $password = "smtp_password";
 
 $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("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?>

Refer ->Sending Mail from PHP Using SMTP Authentication - Example
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
chandana replied to Anoop S on 31-Jan-12 11:50 PM
Thanks for ur reply....but i already tried that code..windows envirnment is work ...but its not work with linux envirenment.
Suchit shah replied to chandana on 31-Jan-12 11:51 PM
PHP send email using PHP SMTP mail Pear functions.

Following code is well commented, you need to make necessary changes as per your setup.



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

kalpana aparnathi replied to chandana on 01-Feb-12 03:21 AM
hi,

<?php
$email_address_to = "recipient@demo.com";
$subject = "Test email subject";
$message_contents = "Hi! This is the content of the test message.";
$header = "From: sender@demo.com\r\n";
$header .= "Reply-To: sender@demo.com\r\n";
$header .= "Return-Path: sender@demo.com\r\n";
mail($email_address_to,$subject,$message_contents,$header);
?>