1

Is it possible to send a mail in PHP without any external packages or tools? If so, is there

any requirement to configure the php.ini file?

Its as follows:

$to = $row->EMail_ID;
$subject = "Reset your password";
$body = "Hi ".$row->Username.", \n\t\t\tA request to reset your  password was received from you. \n\n\n";
$headers = "From: admin@admin.com\r\n"."X-Mailer: php/";
mail($to, $subject, $body, $headers); 

The error i get:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, 
verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in F:\wamp\www\pwd.php on line 20
koopajah
  • 23,792
  • 9
  • 78
  • 104
vgathan
  • 11
  • 2

4 Answers4

0

Sounds like you don't have sendmail (or one of its alternatives, like postfix or courier) installed.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

Either you don't have a mailserver running locally, or it is running with a non-standard setup.

You can look at the output of phpinfo() to get information about your mail system. Specifically:

sendmail_path  /usr/sbin/sendmail -t -i
SMTP           localhost
smtp_port      25

If you are using a shared host, your hosting provider probably has documentation on how to send mail - and that may require you to use a PEAR / 3rd party library.

jasonbar
  • 13,333
  • 4
  • 38
  • 46
0

Yes, of course, as he said above me, but I'll expand upon his example.

$mailto = "receiver@mail.com"
$mailfrom = "sender@mail.com"
$subject = "The subject of the mail"
$content = "The content of the mail"

$headers = "From: $mailfrom\r\n" .
    "Reply-To: $mailfrom\r\n" .
    "Content-type: text/html; charset=iso-8859-1\r\n" .
    'X-Mailer: PHP/' . phpversion();

$mailsuccess = mail($mailto,$subject,$content,$headers);

if($mailsuccess)
{
    echo 'Mail Sent!';
}
else
{
    echo 'Failed to send Mail!';
}

Note that Content-Type entry in the headers. Leaving that off makes it just default to plain text mail, putting that line in sets the content-type to text/html, allowing you to use html in the mail content. You can set the from and reply-to lines to whatever you want as well, and there are other options for the headers as well, such as the Important Flag and all that. Also, if you want to send it by name and address you can do "Fname Lname < address@mail.com >" in the headers.

Phoenix
  • 4,488
  • 1
  • 21
  • 13
  • Oh, you don't have sendmail set up properly. – Phoenix Dec 21 '10 at 14:08
  • And you're on a WAMP server, which means I don't think you have sendmail. You'll need to configure it to use your own SMTP server instead probably. Take a look at this http://www.simplehelp.net/2008/08/25/how-to-install-and-setup-apache-mysql-and-php-in-windows/#sendmail – Phoenix Dec 21 '10 at 14:19
-3

Yes it is possible with mail function which is an internal function.

No you don't have to configure the php.ini

<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// Send
mail('caffinated@example.com', 'My Subject', $message);
?>

If you want to check this on a local server you have to install a mail server.

Mohsen
  • 393
  • 7
  • 9
  • 2
    You can most certainly send email from a local mailhost to another, "online" mail host. – jasonbar Dec 21 '10 at 13:59
  • I need to send such password reset mails from php mail() without PEAR or other localhost web servers such as postcast.... I need to send mails from localhost – vgathan Dec 21 '10 at 14:09
  • I think you should send your password reset email from your website's domain not from localhost. – Mohsen Dec 21 '10 at 14:12
  • You mean you are writing just a "local website" or "you are going to host it" after it will be finished? – Mohsen Dec 21 '10 at 14:18
  • If you want to host it later, install a local mail server to check your application like MercuryMail or Sendmail. Both of them have Windows and Linux versions. When you have your local mail server installed, configure a domain in mail server and add at least 2 email address. First one is your websites email address like no-reply@mysite.local and second is user email address. Then check your password reset code with this two email addresses. – Mohsen Dec 21 '10 at 14:46