sending mail from localhost in php

PHP Add comments

Use the basic phpmailer code which comes with the phpmailer zip form phpclasses.org
the following code is the default one which sends mail from your server by using the default server settings.

the host name is set to localhost and authentication is false(which takes the default). these are the two lines which
vary when sending mail form localhost.

include("includes/phpmailer/class.phpmailer.php");
 
$mail = new phpmailer();
$mail->PluginDir = "/include/";
$mail->IsSMTP(); // send via SMTP
 
$mail->From = "noreplysample.com";
$mail->FromName = "From Name";
$mail->AddAddress("address@host.com");
$mail->WordWrap = 65; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Mail Subject";
 
$mail->SMTPAuth = false; // turn off SMTP authentication			
$mail->Host = "localhost"; // SMTP servers
 
$mail->Body = $msg;
$mail->send();

the code to send mail form your localhost to any remote email server
the difference in the above and the following is i have given the important part which need to send mail from localhost
the first below two lines differ from the above. and the last two lines are added.
you have to give your servers auth information to send mail. So when ever mail is sent from localhost to a remote server
a server reference is needed. which means… mail is sent form localhost on-behalf of your server.

 
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Host = "samplesite.com"; // SMTP servers
$mail->Userame = "testing@testing.com";
$mail->Password = "testing";

so when the above lines gets executed this code connects to your site for verification
and then the mail is sent with reference to your server. so
you need a server by using which you can send mails from localhost.

here is the full code which sends mail from localhost.

include("includes/phpmailer/class.phpmailer.php");
 
$mail = new phpmailer();
$mail->PluginDir = "/include/";
$mail->IsSMTP(); // send via SMTP
 
$mail->From = "noreplysample.com";
$mail->FromName = "From Name";
$mail->AddAddress("address@host.com");
$mail->WordWrap = 65; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Mail Subject";
 
$mail->SMTPAuth = true; 						
$mail->Host = "samplesite.com"; // SMTP servers
$mail->Userame = "testing@samplesite.com";
$mail->Password = "testing";
 
$mail->Body = $msg;
$mail->send();

the from address should be a valid domain string. else mostly your mail will not be sent.

3 Responses to “sending mail from localhost in php”

  1. Henk Valk Says:

    Thanks for sharing this script.
    I’ll see a typo in your form “Userame” must be “Username”

    Greetz

  2. Harpreet Says:

    i am developing a site with zope(2.10.4). I am new to mailhost. i want to generate two automatic emails to send username and password to the user(with python method). Can anyone help me in this regard?

  3. rr Says:

    good…

Leave a Reply

Entries RSS