Warning: fopen(graphic_design/files/thread-740-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened PHP @mail -
PDA

View Full Version : PHP @mail


Jeff O.
04-30-2004, 11:07 AM
I developed a PHP app that I want to send e-mails. I wrote the function below and it works perfectly. My question for you gurus is: Is there a better way of setting the e-mail server than the way I set it here, i.e. - via the ini_set(). I will be delivering this app to a couple of different clients, so my plan is to simply change the ini_set() line for each client's e-mail server.
Much appreciated! :)


<?php

///////////////////////////////////////////////////////////////////////////////////////////////
//
// Name: email_functions.php (Functions)
// Author: Jeff Ocheltree
// Date: 12/1/2003
// Purpose: PHP e-mail functions.
// Revisions:
//
///////////////////////////////////////////////////////////////////////////////////////////////

function send_email($to,$subject,$message,$header)
{
//I set the mail server here. This is a PHP ini file setting, but I set it here dynamically with ini_set(). The mail server setting may be an issue when porting the app to different servers, primarily because of firewall configurations.

ini_set("SMTP","mail.mycompany.com");

if (@mail($to,$subject,$message,$header)) {
//E-mail sent successfully.
return true;
}else{
//E-mail failure. Probably target domain not found.
return false;
}
}

?>

Arch Stanton
04-30-2004, 11:33 AM
I don't know, I use a php script modeled after the famous Perl Formmail

http://www.dtheatre.com/scripts/formmail.php

Jeff O.
04-30-2004, 11:51 AM
Jack's FormMail.php looks pretty cool. I guess I'm kind of on the right track with my function listed above though. I'll just have to hope that my client's have their php.ini SMTP setting designated correctly, or I'll have to set it manually for each client in my function.

Thanks Joel. :)

Koobi
05-20-2004, 11:00 AM
er this is nothing compared to that last link heh heh, but I thought I'd just post a slightly altered version of your script Jeff O :)



<?php
function sendEmail($to, $subject, $message, $header)
{
@mail($to, $subject, $message, $header) ? $status = 1 : $status = 0; // This is known as the ternary syntax
return $status;
}

$sendTo = "someone@mail.com";
$sendSubject = "Hey there";
$sendMessage = "My message";
$sendHeader = ""; //Whatever the header is
$handler = sendEmail($sendTo, $sendSubject, $sendMessage, $sendHeader); // Calls the function
if($handler)
{
$msgOut = "E-mail was sent";
}
else
{
$msgOut = "E-mail was NOT sent";
}
echo $msgOut;
?>



Ternary syntax is useful for one-liner if's like that. The only problem is that they are not easily readable. Also, I added an extra parameter where you can pass the SMTP settings into the function :)

Jeff O.
05-20-2004, 03:40 PM
Yeah the Ternary syntax is cool. Thanks. You say you added an extra param for the SMTP settings. Unless I'm just too tired to see straight right now, I'm not seeing that.

Thanks Bane. Very cool of you! :bowdown:

Octane
05-21-2004, 09:05 AM
only problem with the function above is that you have to add the headers each time to send HTML.


## re-formats mail program for easier use
function send_mail ($to, $from, $subject, $body, $html=0, $additionalheaders='')
{
$headers = '';
if ($html == 1)
{
$headers .= "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= $additionalheaders ? $additionalheaders : '';
return (mail ($to, $subject, $body, $headers));
}
else
return (mail($to,$subject,$body,"From: $from"));
} // end send_mail

Jeff O.
05-21-2004, 01:14 PM
Thanks Octane. Great addition with the headers. Very helpful for me. I guess my original concern in this thread, the SMTP setting for the customer's environment, is not as big an issue as I originally thought. I've primarily been an ASP developer for the last 5 years, and I've only been doing PHP for about a year, so I'm still learning the finer points. You guys have been really helpful (Octane, Joel, Bane)!

Thanks! :bowdown: