User Contributed Notes: mail
thomas@liga.com
02-Feb-1999 07:54
If you want to use special nordic character in a mail() function,
then you must add extra header information about this. Try this (all on a single line):
<pre>
mail("$to","$subject","$message","From: $from
\nContent-Type: text/plain; charset=iso-8859-1
\nContent-Transfer-Encoding: 8bit");
</pre>
garretg@otable.com
24-Feb-1999 06:56
Separating extra mail headers with a carriage return and a linefeed
<PRE>\r\n</PRE> seems to be more standard that using just a single linefeed.
sasha@uroam.com
26-Feb-1999 06:17
To integrate qmail in mini-qmail config I set sendmail_path
in php3.ini to /var/qmail/bin/qmail-inject
lew@persiankitty.com
07-Mar-1999 03:57
To handle bounced mail, I believe you can specify the "bounce" address in your headers section, as follows:
"Errors-To: lew@persiankitty.com"
Someone should read the mail RFC's to make sure...
Also, I have found that some Japanese mailers do not conform to the RFC's. Email addresses are supposed to be case INSENSITIVE. However, some Japanese mailers insist on UPPERCASE ONLY usernames. Strange.
ylee@monet.csc.vill.edu
07-Mar-1999 09:41
I wrote a simple class to test attaching a file in email using MIME. You can see it
<a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Frenoir.vill.edu%2F~ylee%2Fmailfile.html%26quot%3B%26gt%3Bhere%26lt%3B%2Fa%26gt%3B%3C%2Ftt&y=1999>
tbrown@pop.nwnexus.com
24-May-1999 02:33
It seems that, at least on my system,
multiple email addresses in the 'to'
parameter need to be separated with
a comma, not a space.
moron@industrial.org
08-Jul-1999 12:42
The message will be sent as the owner of the web process so be aware that some over zealous spam catchers will stomp on messages using a "message from:" header of "nobody". Not sure if you can tell PHP to use an alternate user for the command line but something to be aware of.
roosto@whoride.com
10-Jul-1999 04:50
If you need ot attach a real name to an email address so that it shows up in the from/to/cc fields. I've found that this format seems to work:<PRE>mail($toName." <".$toAddress.">", $subject, $message, "From: ".$fromName." <".$fromAddress.">")</PRE>
scott@audiohighway.com
30-Jul-1999 03:32
If you don't trim the subject line like:
<PRE>$subject=trim($subject);</PRE>
You can wind up with the from field in the body of the message.
It would be nice if the call to mail() did this for you automatically.
adconrad@0c3.net
19-Aug-1999 03:20
If you want to (god knows why, maybe you have clients who all use Outlook or Nescape Mail or something) send HTML formatted messages using the mail() function, try something like this:
<PRE>mail($ToName." <".$ToAddress.">", $Subject, $message, "From: ".$FromName." <".$FromAddress.">\nContent-Type: text/html; charset=iso-8859-1");</PRE>
jm@beats.com
22-Aug-1999 09:53
MAKE SURE your server's php.ini file has the "sendmail" variable set. I continue to forget this point until sendmail mysteriously seems to not work after the mail() function returns just fine.
on UNIX, it's <tt>/usr/lib/sendmail -t</tt>..don't forget the <tt>-t</tt>--it caused me another hour of cluelessness.
firm@iname.com
31-Aug-1999 12:45
Still having troubles while sending messages via mail () to sendmail-powered machine? Just add "Return-Path: <something>" as additional header.
studioq@chorus.net
02-Sep-1999 06:58
I wrote up a simple script to illustrate use of mail() and fread(). Body of msg is from a text file. Get the source code <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fwww.chatpic.com%2Flibrary%2Fphp3%2Fmail%2Fsend_mail.htm%26quot%3B%26gt%3Bhere.%26lt%3B%2Fa%26gt%3B%3C%2Ftt&y=1999>
npy@nicolas.net
17-Sep-1999 09:05
The only way to have the From field filled
is to rewrite the mail function Ex:
function n_mail( $from, $to , $subject,$core_msg, $add_header)
{
$cmd_line=sprintf("/usr/sbin/sendmail -r %s -t",$from);
$fp=popen($cmd_line,"w");
$temp=sprintf("To: %s\n",$to);
$len=strlen($temp);
$ret_p=fputs($fp,$temp,$len);
$temp=sprintf("Subject: %s\n",$subject);
$len=strlen($temp);
$ret_p=fputs($fp,$temp,$len);
if ($add_header != "")
{
$temp=sprintf("%s\n",$add_header);
$len=strlen($temp);
$ret_p=fputs($fp,$temp,$len);
}
$temp=sprintf("\n%s\n",$core_msg);
$len=strlen($temp);
$ret_p=fputs($fp,$temp,$len);
pclose($fp);
}
There are no tests here ... not enought place
but testing the returns is better ....
cchsieh@csie.nctu.edu.tw
30-Sep-1999 12:22
To have the From: field filled only with email address, try the following:
<pre>mail($mailto,$mailsubject,$mailbody, "From: <".$mailfrom.">\r\n" );</pre>
Elite@LibAir.com
03-Oct-1999 04:25
The mail function is very useful, but for alot of people that have asked me, It is a bit cunfusing on how to format it, as in recognizing the individual functions. Below is the easiest format for beginners to follow:
<pre>
MAIL(
"Elite@LibAir.com",
"Feedback from PHP3 Mailing Page...",
"Name - $name
Email - $email
Comments - $comments",
"From: $email\nX-Mailer: PHP/" . phpversion());
</pre>
-Andrew Heebner-
eric@afm.nu
06-Oct-1999 06:04
Having problems with swedish characters
like: едц (might not show up right here). Theese chars have different ascii codes on mac and pc. I've tried to use this:
mail("$to","$subject","$message","From: $from
\nContent-Type: text/plain; charset=iso-8859-1
\nContent-Transfer-Encoding: 8bit");
but this does not do the trick. Any suggestions?
ik3fan@inca.dei.unipd.it
13-Oct-1999 12:58
For win32 users: remeber to correctly setup the php.ini file BEFORE using the mail() function. Edit the [mail function] section and set the following 2 var according to your needs: SMTP=your.stmp.outgoing-mail-sercer, sendmail_from=YourUser@whatUwant.domain
richard.heyes@heyes-computing.net
23-Oct-1999 07:46
To have names appear in the To: and From: lines of mail clients such as Outlook, Outlook Express etc I've used the following which worked quite well:
<PRE>
$to = "$toname <$toemail>";
$from = "$fromname <$fromemail>";
$subject = "Your Subject";
mail($to, "$subject", $message, "From: $from\n");
</PRE>
The $toname, $toemail, $fromname, $fromemail don't actually have to be variables, but since you'll probably use variables there (defined elsewhere in your scripts), I put them in.
HTH
rolf@cable-modems.org
14-Dec-1999 01:17
<PRE>mail("rolf@cable-modems.org", "test subject", "test body 1");</PRE>
Does not work, but:
<PRE>mail("rolf@godset.dk", "test subject", "test body 2");</PRE>
works okay. This needs a little further explanation in the documentation. If the dash and other special characters, that are perfectly legal in email addresses, needs to be escaped or something - then that should be in the documentation.
cap@capsi.cx
15-Dec-1999 10:50
The reason people often ask for a perl-alike mail method is probably because they don't want to add an entire message on one line. You don't need to:
<pre>
$msg = "Hello there,\n\n";
$msg .= "Line2\n"; // appends a line
$msg .= "Another line\n\n";
$msg .= "Bye!";
</pre>
Quite easy and still readable.
peter@alienalchemy.com
20-Dec-1999 04:31
Here is a complete mail program I have written that has a lot of features. You can use it as-is or you can read through the code and get some ideas on how to implement custom mail solutions.
http://askpete.alienalchemy.com/v2/freeware.shtml
flec@flec.co.uk
30-Dec-1999 07:39
ylee@monet.csc.vill.edu & Dan Potters' CMailFile class works great. It would be nice if something like this was included as a part of PHP in the future.
peter@alienalchemy.com
15-Jan-2000 06:51
Add additonal headers for the return path and x-sender which I think does the trick.
"X-sender: addressyouwant@yourdomain.com"
"Return-Path: addressyouwant@yourdomain.com"
I released a new version of web email it is at 1.6.1 at http://alienalchemy.com/webemail
Added form field verification, email address verification, and referer verification prior to sending email through the script as well as additonal mail headers for x-sender, return-path etc...
tworks@troutworks.com
24-Jan-2000 01:42
I think I got rid of sendmail completely in favor of qmail, and the wrapper doesn't work. Try using /var/qmail/bin/qmail-inject instead.
php@ganzeinfach.de
29-Jan-2000 11:33
how to set e.g. Return-Path: <nobodyhome@myserver.com> in a script that sends mail? changing "From:" or "Reply-to:" is _not_ the solution.
fputs($fd, "Return-Path: nobodyhome@myserver.com \n"); also takes no effect.
thx
g@gknow.com
18-Feb-2000 01:16
ralph:
mail("ralph@snarff.com", "this is the subject", "this is the body","From: g@gknow.com\ncc:swampy@theswamp.com");
this CCs someone. put the "\n" between recipients
randy@iduh.com
24-Feb-2000 08:36
I modified a little mail script and put it in a package...if anyone cares.
http://iduh.com/mailit
adm@imexis.com
27-Feb-2000 03:53
If you are going to send email in the form:
<pre>
$to = "$toname <$toemail>";
</pre>
make sure you strip commas and semicolons from $toname, otherwise mail() will think you are sending to more than one address.
scross@san.rr.com
01-Mar-2000 09:39
I haven't seen it mentioned yet, but if you want to set a priority on your
message, add this:
X-Priority: #
Where # is anywhere from 1-5, 1 being the highest, and 5 being the lowest.
zeth@zethw.com
03-Mar-2000 10:56
You can do a blind carbon copy (Bcc). Its the same syntax as in sendmail:
<PRE>if (mail("MailingList@$SERVER_NAME", trim($subject),
> trim($message), "From:
> maillist@$SERVER_NAME\nReply-To: maillist@$SERVER_NAME\nErrors-To:
> maillistErrors@$SERVER_NAME\nBcc: $customerEmails\nX-Mailer: PHP/" .
> phpversion())) { </PRE>
What the person receiving the mail will see in the To: field is maillist@SERVER, but it will send it to everyone in the comma delimited list called $customerEmails
jrp17@cornell.edu
07-Mar-2000 07:12
Alright, noone seemed to be able to solve the problem of PHP sending mail from "nobody@myhost" so I decided to make a little work around. I hope this helps people a bit.
<PRE>
$fp = popen('/usr/sbin/sendmail -t', "w");
fputs($fp,"To: $to\n");
fputs($fp,"From: $email\n");
fputs($fp,"Subject: $subject\n\n");
fputs($fp,$message);
fputs($fp,"\n");
pclose($fp);
</PRE>
shill@waitingforbob.com
09-Mar-2000 03:08
The email checker by m@1a.dk fails on email addresses that have more than one component before the TLD (like somewhere.co.uk, but also something like math.college.edu). This should work better:
<PRE>
if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-\.]+)\.([a-z]{2,3}$)", $email, $check)) {
if (getmxrr($check[1] . "." . $check[2]) {
return "Valid";
}
return "No MX for " . $check[1] . "." . $check[2];
} else {
return "Badly formed address";
}
</PRE>
adm@imexis.com
10-Mar-2000 01:55
Want to know how to configure the Return-Path header?
Say that you usually send mail with the PHP mail() function as such:
mail($toaddress,$subject,$body,$headers);
where $headers includes "From: $fromaddress\n" among other things.
use this function
<pre>
function mailfrom($fromaddress, $toaddress, $subject, $body, $headers) {
$fp = popen('/usr/sbin/sendmail -f'.$fromaddress.' '.$toaddress,"w");
if(!$fp) return false;
fputs($fp, "To: $toaddress\n");
fputs($fp, "Subject: $subject\n");
fputs($fp, $headers."\n\n");
fputs($fp, $body);
fputs($fp, "\n");
pclose($fp);
return true;
}
</pre>
Make sure that special characters are removed from $toaddress and $fromaddress as they are being passed to the shell. The email validation function from the PHP mailing list does a good job of removing them, except for backtick (`). The return value of the function is basically meaningless as sendmail sends errors to STDIO and I don't know how to handle that. Also, this may insert an X-Authentication-Warning header in your email.
alex@radionet.psi.br
13-Mar-2000 05:27
The program to send message with attached files, can be located in
http://www.radionet.psi.br/alex/sendmail/
tstrum@salter.com
29-Mar-2000 08:34
OK, I was totally confused about the email validation code snippits from above. In my testing both were buggy. The following code seems to work perfectly well for the domains I listed below in the function calls. This includes hyphenated domains.
<pre>
function checkmail ($email) {
if (eregi("^[_\.0-9a-z-]+@([0-9a-z][-0-9a-z\.]+)\.([a-z]{2,3}$)", $email, $check)) {
if (getmxrr($check[1].".".$check[2],$temp)) {
return "Valid - ".$check[1].".".$check[2]." ";
}
return "No MX for " . $check[1].".".$check[2]." ";
} else {
return "Badly formed address. ";
}
}
echo checkmail("user@triple-bypass.com");
echo checkmail("user@salter.com");
echo checkmail("user@ns.sympatico.ca");
</pre>
mlm@metrolink.com
30-Mar-2000 08:07
sendmail_path should probably be "sendmail -t -i" instead of "sendmail -t". The -i means not to interpret a single '.' on a line by itself as the end of a message. With "sendmail -t" all your messages that include a single '.' on a line by itself will be truncated.
mikey177@networkone.net
07-Apr-2000 06:13
Need to specify a "Form" address when sending email? Here is a small working script that will allow you to specify the from address along with the subject, body, and headers that will work with PHP v4.0b4.
------------start code---------------
<?php
// switch the value of $email to the
// persons email address you are sending
// this to.
$email = "username@myisp.com";
$subject = "Working PHP v4.0b4 mail script";
$body = "Um, looks good from here.";
$headers = "From: mymailer@mydomain.com\n";
$headers .= "Reply-to: noreply@mydomain.com\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Mailer: CBAS v1.0.09\n";
mail ($email, $subject, $body, $headers);
echo "schweeeeeet.....its workin'";
?>
--------------end code--------------
skillick@netexpress.net
12-Apr-2000 10:28
After much research, and more than a little time spent, I have found an answer to the "problem" of headers in received mail like From:nobody@localhost.
RFC822 makes it plain that the header "Return-path" is intended to be added to the message by the mail program at the destination(POP3), not at the source(SMTP). Adding it as a header to a mail() function will have no effect as mail() is the source.
If you have PHP compiled into Apache, then PHP mail() sends email to sendmail as the owner of the process which, of course, is 'nobody'. So sendmail will use 'nobody' in the "From" field of the sendmail generated portion of the header.
The solution is to follow the instructions in the sendmail configuation to reverse-map outgoing mail to "somebody@yourserver". Here is the URL to those instructions: http://www.sendmail.org/virtual-hosting.html
Follow instructions 6 and 7 and you will have no further problems.
I will be changing my FormMail script to reflect this info soon. Good luck.
Hylian@Zelda-Temple.com
13-Apr-2000 10:34
To Validade Email addresses, you can use the code below:
<?PHP
if (!ereg(".+@.+\\..+", $from)) {
echo "<CENTER>Invalid Email address.</CENTER>";
} else {
mail($to, $subject, $message, $from_header);
}
?>
paul@pilat.com
17-Apr-2000 12:03
My main problem was that the fqdn of the machine wasn't available via RDNS - therefore was being rejected as sendmail attempted to connect with a MAIL FROM: nobody@machine.mydomain.com.
I managed to get round it by editing /etc/httpd/conf/php3.ini and changing the sendmail command line to be 'sendmail -t -f validaccount@mydomain.com' - bounces will be sent to this addy, so keep it a live one, and any "Return-Path" headers in the mail() command are ignored, but the "From:" headers take over as far as the end client is concerned.
markus@dnet.it
28-Apr-2000 08:07
It's interesting to see, how my first post for a regular expression for e-mail checking some months ago (on the ereg() manual page) evolved over time... This was my original check (simple but correct):
<pre>
eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)
</pre>
and these is the "evolved" one with additions thanks to r.garcia@netia.net:
<pre>eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$", $email)
</pre>
All other contributions are broken in some way and fail to validate correct addresses as: 123-@xxx.com, _a_@xxx.com or a@aa.bb.cc.dd.co.uk.
Hope this helps!
Markus Senoner
tgpsolutions1@megapathdsl.net
03-May-2000 06:37
This validation code fixes the problem with not being able to recognize long MX records such as someone@ab.cd.ef.gh
<tt>// validate e-mail address
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$", $email, $check)) {
if ( getmxrr(substr(strstr($check[0], '@'), 1), $validate_email_temp) ) $email_valid = 1;
}</tt>
flec@flec.co.uk
05-Jul-2000 02:41
Be careful when sending results of a form/textarea when using mail(). If, for example, sendmail encounters a \n.\n (dot on it's own line) instance it will take this to mean the equivalent of an SMTP EOM command, and you'll end up with only half of your mail. In fact, it'd probably be better if the php.ini file came with "sendmail -ti" as default, instead.
igor@obraz.net
19-Jul-2000 04:34
To have a real name attached to the
"From:" string simply use "my name <my email>" notation method.
badger@noemata.net
22-Jul-2000 10:09
I had problems using mail() - only some mailadresses/servers received them. The problem seem to have been solved by adding the "Return path" header to the mail.
isthar@yahoo.com
27-Jul-2000 04:49
Of course bcc won't work with mail() sometimes!
From 'man sendmail':
"...
-t Read message for recipients. To:,Cc:, and Bcc: lines will be scanned for recipient addresses. The Bcc: line will be deleted before transmission.
..."
and -t is the default option for php...
dowen@vetica.com
31-Jul-2000 06:41
mail() will not work with Qmail unless you make a symlink for old sendmail compatibility. ie. ln -s /var/qmail/bin/sendmail /usr/lib/sendmail and ln -s /var/qmail/bin/sendmail /usr/sbin/sendmail
I think there is a ./configure option to fix this. But this works.
bens@saber.net
02-Aug-2000 08:32
Want to send mail thru isp's mail server instead of thru localhost?
I've figured out how to do it, and submitted a code snippet at phpbuilder.com - in the code library, under "networking".
Hope this helps - lots of notes on this above!
Ben
cfchung@gol.com
08-Aug-2000 02:03
Thanks to Markus Senoner for his contributions on the mail validation code. For ezmlm however, email addresses such as news-subscribe-cfchung=gol.com@slashdot.org (for example) is a valid email address. Here is the eregi code for ezmlm.
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*=([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$", $addr)