How to send emails from localhost

For beginners in the field of web development, we usually find ourselves starting out with the XAMPP package to develop using PHP/MySQL. There are times that we need to send emails from our web applications using the PHP mail() function.

If you have access to your ISPs SMTP server. you may configure it in your PHP configuration file. You may use phpinfo() to help you find your configuration file. (in my xampp installation it is at F:\xampp\apache\bin\php.ini)

In your PHP.ini find the ff: lines and modify them using your ISPs SMTP server.

In this example I am using PLDT’s SMTP server. You may surf the web for SMTP servers of other ISPs like the one I found here

[mail function]

SMTP = smtpdsl4.pldtdsl.net

smtp_port = 587

 

For some cases, your ISP blocks you from using their SMTP servers, for those cases you may still be able to send emails using a gmail account. For this you need to have newer versions of XAMPP since older XAMPP packages have old sendmail files. You may still use an old XAMPP version if you replace the default sendmail folder that came with your package with a new sendmail folder from new XAMPP packages.

 

In your PHP.ini find the ff: lines and modify them

[mail function]

;SMTP = smtpdsl4.pldtdsl.net
;smtp_port = 587

sendmail_path = “F:\xampp\sendmail\sendmail.exe -t”
mail.add_x_header = Off

 

In your sendmail.ini (mine is at F:\xampp\sendmail\sendmail.ini)

find the [sendmail] block and add the ff: values.

You will need to input a gmail email address and password so I strongly suggest that you create a dummy account for this purpose.

DO NOT USE YOUR PERSONAL EMAIL ADDRESS

[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=yourDummyGmailAccount@gmail.com
auth_password=yourDummyGmailAccountPassword
force_sender=myemail@gmail.com

 

restart apache and then try to send a test email

 

$headers = “Reply-To: noreply@mydomain.com\nFrom:noreply@mydomain.com\nContent-Type: text/html;charset=iso-2022-jp\nX-Mailer: PHP/” . phpversion();
$subject = “test email”
$to = “recipient_email@yahoo.com”;

$body = ‘test’;
echo mail($to, $subject ,$body , $headers);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s