Sending mail with PHP

7 posts Page 1 of 1
Contributors
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

Sending mail with PHP
AnoPem
Hello fellow coders today i will learn you how to send emails with php. sending emails from your website can be done if your webhost supports it, you cannot test the email function on localhost.

first of, create a new php file. add this code
Code: Select all
//Getting senders email with post from form.
$from = $_POST["from"];

//Where the email is sent.
$to = 'recipient email';

//The subject of the message.
$subject = 'This is my subject :o';

//The message.
$message = 'This is my message and it can contain html';

//Configuring the header, read why below.
$header  = 'MIME-Version: 1.0' . '\r\n';
$header .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$header .= 'from: ' . $from;

//This will send the mail with the information we have provided.
mail($to, $subject, $message, $header);
Why we configure the mail headers, from PHP.net
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). Validate parameter not to be injected unwanted headers by attackers.
https://t.me/pump_upp
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Sending mail with PHP
Shim
Nice one by the way you should add this header : Content-Transfer-Encoding:8bit
Find my programs on Softpedia
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Sending mail with PHP
smashapps
Nice tutorial.

To expand on what you send about not being able to send from localhost, by default you can't. You can change the php.ini file and configure the smtp server settings to whatever server you are sending from, for example if yuo are sending from a gmail account, configure the smtp settings to gmail's smtp server, then you should be able to send emails fine.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

Re: Sending mail with PHP
AnoPem
smashapps wrote:
Nice tutorial.

To expand on what you send about not being able to send from localhost, by default you can't. You can change the php.ini file and configure the smtp server settings to whatever server you are sending from, for example if yuo are sending from a gmail account, configure the smtp settings to gmail's smtp server, then you should be able to send emails fine.
Thank you i was not aware of that cooll;
https://t.me/pump_upp
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Sending mail with PHP
XTechVB
The mail function works reliably only if the sender email is a real existing email address and also it has to be on the same server as the website. I did a little experiment with three email addresses, one from yahoo, one from gmail, and one from the same server my website was on. Only the last one worked and actually sent the email.
You can find me on Facebook or on Skype mihai_92b
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

Re: Sending mail with PHP
AnoPem
XTechVB wrote:
The mail function works reliably only if the sender email is a real existing email address and also it has to be on the same server as the website. I did a little experiment with three email addresses, one from yahoo, one from gmail, and one from the same server my website was on. Only the last one worked and actually sent the email.
I have used multiple emails and im currently using gmail, its working fine for me. So my guess is its something with your host
https://t.me/pump_upp
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Sending mail with PHP
XTechVB
I have used multiple emails and im currently using gmail, its working fine for me. So my guess is its something with your host
Yeah apparently, also i mistyped something in the headers.
You can find me on Facebook or on Skype mihai_92b
7 posts Page 1 of 1
Return to “Tutorials”