group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
108 Members
Join this group to post and comment.
Hari Prasaanth
Hari Prasaanth
Computer Science
02 Jan 2019

PHP Mail Function

Can You explain me about how to send mail through php mail function?

4mos ago

Assuming that you have the basic configuration done for mailing, you could simply use the PHP mail function as follows -

mail( $to, $subject, $message, $headers, $parameters); 

Headers and Parameters are optional, and need to be used depending upon the kind of functionality you need. Does this answer your question?

PS: I'd recommend using a dedicated library for mailing - PHP Mailer. 

Manish Goyal
Manish Goyal
Computer Science
4mos ago

mail function won't work on localhost, you can use phpmailer library to send emails using 3rd party smtp such as gmail, sendgrid or elastic

Naveen Sunil
Naveen Sunil
Information Technology
4mos ago

Use mail() and be sure to add headers like mailer, from, reply to, to, if possible add security headers. This function doesn't work on localhost if it doesn't connect to outbound port. If you have setup port forwarding or dynamic dns it will work.

Shruthi Pillai
Shruthi Pillai
Information Technology
4mos ago

The mail() function allows you to send emails directly from a script. 

EG: mail($to,$subject,$message,$headers);

DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
4mos ago

First you need to download the library PHPMailer. create a new folder where you have your index file and copy paste the files after downloading to the new folder. You need to specify the Gmail account where you want to send gmail. 

require_once('PHPMailer/PHPMailerAutoload.php');

$mail = new PHPMailer();

$mail->isSMTP();

$mail->SMTPAuth = true;

$mail->SMTPSecure = 'ssl';

$mail->Host = 'smtp.gmail.com';

$mail->Port = '465';

$mail->isHTML();

$mail->Username = 'your email address';

$mail->Password = 'gmail password';

$mail->SetFrom('No-reply');

$mail->Subject = 'Hello World';

$mail->Body = ' An email example';

$mail->AddAddress('destination address');

$mail->Send();

?>

By running the above code you can send image to the receiver.