PDA

View Full Version : Send Email Function



amandeep.sandhu
Jun 1st, 2006, 11:32 AM
function sendMail($to, $name, $subject, $from, $message)
{

$headers ="MIME-Version:1.0\r\n";
$headers .="content-type:text/html; Charset=iso-8859-1\r\n";
$headers .= "From: " . "$name" . "<" . "$from" . ">\n";
$headers .= "Return-Path: <" . "$to" . ">\n";
$headers .= "Error-To: <" . "$to" . ">\n";
$headers .= "X-Sender: <" . "$to" . ">\n";
$headers .= "X-Mailer: PHP v".phpversion()."\n";


$subject= nl2br($subject);
$message=nl2br($message);
$to=$to;
$mail= mail($to, $subject, $message, $headers) ;
return $mail;
}

// How To Use

$to="[email protected]";
$from="[email protected]";
$name="karan";
$subject="hello";
$message="How r u";

sendMail($to, $name, $subject, $from, $message);

You can Place function sendMail in a separate file like functions.php and then include functions.php in your current file and call function sendMail in other file. With this if you have to send email in multiple forms you have to call sendMail($to, $name, $subject, $from, $message); only.

You can Pass $to, $from , $name, $subject, $message to this function and send email to anybody.

Thanks.

d11wtq
Jun 1st, 2006, 01:24 PM
Handy if you just want basic mail sent with the correct headers :)

I've recently written something a bit more powerful (http://www.swiftmailer.org/) but it would be a bit of an overkill unless you're sending several emails ;)

fci
Jun 1st, 2006, 01:28 PM
and another:
http://pear.php.net/package/Mail/redirected

amandeep.sandhu
Jun 3rd, 2006, 08:20 PM
and another:
http://pear.php.net/package/Mail/redirected

The email function i have contributed is easy o understand and edit. Its aim is to just send simple email.

d11wtq
Jun 3rd, 2006, 08:37 PM
The email function i have contributed is easy o understand and edit. Its aim is to just send simple email.

Indeed :) Looks good.

ahallicks
Dec 6th, 2007, 03:43 PM
THanks for that. I've used a modified version of this that allows me to E-Mail all of the members of my site. Cheers, was very simple to use.

fl00d
Dec 6th, 2007, 10:06 PM
Hopefully newer users will see this and the number of posts titled "HELP WITH MAIL()" or something similar will diminish :D

Perhaps you can add in a notification whether the email was sent or not. Still keeps things simple :)

jhbalaji
May 12th, 2010, 05:52 PM
Handy if you just want basic mail sent with the correct headers :)

I've recently written something a bit more powerful (http://www.swiftmailer.org/) but it would be a bit of an overkill unless you're sending several emails ;)

Your Swift mailer package is too good!
I started to use that now :)