Tuesday, May 20, 2008

Send Mail From Local Machine (Configuration)

Many developer today like to test php mail script from localhost. Dislike to upload script in server for test. Moreover, like to finalize everything from localhost/own development platform. You must need an internet connection. Furthermore need to configure your php.ini file what send mail using local SMTP.

A number of php.ini files are there in PHP, MySql and Apache. Which one you have to configure? Definitely not everyone. Mail as well as SMTP are very closely and intimately related with apache (web server). So you have to configure php.ini under apache\bin\php.ini. Just follow the following instructions: Change the configuration of php.ini file accordingly:

Default: ;SMTP = localhost Change To : SMTP = "mail.gmail.com" Effects/Reason for Change: Give outgoing mail address

Default: ;sendmail_from = me@example.com Change To : sendmail_from = novice.devlpr@gmail.com Effects/Reason for Change: Default mail address where from this mail sent.address

Default: max_execution_time = 30 Change To : max_execution_time = 90 Effects/Reason for Change: Sometimes this execution takes time more then 30 sec.

Don’t forget to remove the beginning semicolon (;) each line which you have changed. Semicolon means comments here.

Now use mail() function to send an Email from localhost.

Example:-

$MailTo="novice.devlpr@gmail.com";
$MailFrom="Novice developer";
$MailFromEMail="novice.devlpr@gmail.com";
$Subject="Test mail from Localhost";

$MailBody="Message: Testing Mail from Localhost You can write here .";

$Headers = 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Headers .= "From:".$MailFrom."<".$MailFromEMail.">\r\n";
echo $MailBody;

if(mail($MailTo,$Subject,$MailBody,$Headers))
echo "Mail sent Successfully.";
else
die("Sorry! The mail not sent successfully.");

Hope now you are able to send email from your local pc with the help of internet connection.

No comments: