一个发送E-MAIL的脚本也许是你能够在Web 站点上找到的最普通的脚本之一了,虽然它很简单,一个邮件脚本有时会令程序员非常沮丧.PHP中有一个叫做mail()的函数,它只需要知道接收方的地址和信件主体就可以发送邮件,但是要让mail()按你的意思运行你还需要解决一些棘手的问题.
要使mail()能够运行,你必须有一个SMTP服务器以便PHP能够连接上去.不管这个服务器对邮件程序多么重要,大多数人对它如何运转没有一星半点的概念.在本教程中,我们将揭示SMTP的秘密并解决一些使用PHP发送邮件的常见问题.而本文中的另一些主题将包括循环遍历一个地址列表的方法以及同时以文本和HTML格式向接收者发送一个邮件的方法.
SMTP是简单邮件传输协议(Simple Mail Transfer Protocol)的缩写,而一个SMTP服务器是一台运行这种协议并发出邮件的计算机.运行这种协议实际上指的是运行诸如Sendmail和Qmail的程序–如果你使用的是一台非Windows计算机的话.在Windows平台上,作为Windows NT Service Pack一部分或是内建于Windows 2000内部的SMTP服务程序就是典型的这种程序.
我并不是说SMTP软件包就只有那些,但是它们是最普通的.如果你的Web站点使用了互联网服务提供者的主机软件包(Internet Service Provider's virtual hosting package)的一部分,那么SMTP服务程序就应该已经在这台计算机上安装了.如果你是一台位于ISP或是室内的计算机的系统管理员,那么你很有可能已经在这台计算机上安装了某种SMTP软件,用来处理从Web服务器发送邮件的过程.
但是,如果你是一个个人用户并只有一个开发用的Web服务程序运行在你的个人电脑上,你可能没有在自己的机器上运行SMTP软件.下面是一个非常简单但是准确的拇指定律:如果你是一个Windows用户而从没有看见SMTP服务器这样的字眼,那么你就没有运行这个程序.如果你确实没有,那么你有两种选择:安装,配置,和维护一个SMTP服务程序(如果你不知道那是怎么一回事的话就不推荐你使用这种方法)或是使用一个现存的SMTP服务器.
"如果一个服务器现在没有运行的话,我又怎么使用它呢?"你可能要问.如果你的计算机是通过拨号连接(或是DSL或电缆)连到互联网的话,你可以使用你ISP的外发邮件服务器.例如,如果你开发用的 计算机是一个Windows98的系统并使用56kbps modem通过EarthLink连接到互联网,那么你就可以使用mail.earthlink.net作为你的SMTP服务器.不论你对你的外发邮件服务器使用什么样的邮件客户端(Eudora, Outlook, Netscape Mail等等),它们都将和你的PHP代码使用你的SMTP服务器的过程一样.技巧就是使PHP知道一点点事实.
mail()函数是非常简单的:只有五个参数,而其中的两个是可选的.这些参数是:
接收方地址,主题,信件内容,其它文件信息头(可选)
SMTP服务程序的其它配置选项(可选)
附加的头参数控制了诸如CC, BCC, Reply-To之类的邮件功能,或者其它遵循SMTP协议的功能.在这个例子中,我只使用From 和 Reply-To信息头.
如果你想给我发邮件但你使用的是一个非Windows系统,程序代码就应该如下:
- <?
- $to = "julie@thickbook.com";
- $subject = "ZDNet Developer article";
- $msg = "I completely understand SMTP servers now!";
- $headers = "From: me@mycompany.com Reply-To: someoneelse@mycompany.com";
- $config = "-fwebmaster@mycompany.com";
- mail("$to", "$subject", "$msg", "$headers", "$config");
- echo "finished!";
- ?>
如果你使用的是一个基于Windows的SMTP服务,那么你有可能不需要使用第五个参数,而且在附加的头信息参数中(也就是第四个参数),你需要将它们分开写–使用 而不是 .所以,同样的一个邮件通过基于Windows的SMTP服务发送的代码如下:
- <?
- $to = "julie@thickbook.com";
- $subject = "ZDNet Developer article";
- $msg = "I completely understand SMTP servers now!";
- $headers = "From: me@mycompany.com Reply-To: someoneelse@mycompany.com";
- mail("$to", "$subject", "$msg", "$headers");
- echo "finished!";
- ?>
脚本中的echo语句使你的Web浏览器在脚本运行完成的时候向你显示一条信息.如果不写echo语句,你就会得到一个"空文件"的对话框,因为没有任何输出能够发送到浏览器端.
只要能够连接到指定的SMTP服务器mail()函数就会返回真值.但是这并不意味着邮件成功的到达了接收方那里.mail()函数不会等待或报告SMTP服务器发来的成功/错误代码.
mail()函数有可能返回假值,然后给你警告"无法连接,在第x行"或是"未知的错误,在第x行."如果这两条信息中的任何一条出现了,你就应该检查php.ini中的SMTP值.引起这些消息有两种可能:SMTP服务器瘫痪了,或者PHP无法连接到它.无论是这两种情况中的哪一种,你的邮件都无法被发送出去.
波比源码 » 用PHP发电子邮件
order generic levaquin 500mg levaquin 250mg without prescription
order dutasteride online cheap buy flomax 0.2mg without prescription zofran 4mg brand
spironolactone 100mg drug buy aldactone for sale fluconazole 100mg pills
purchase ampicillin generic ampicillin order erythromycin cost
order fildena 100mg without prescription nolvadex cost buy methocarbamol 500mg without prescription
order lamotrigine buy minipress 2mg retin cream tablet
cheap tadalafil 10mg tadalis 20mg drug buy voltaren generic
indomethacin 50mg generic suprax buy online brand trimox 500mg
cialis 20mg sans ordonnance en france pharmacie en ligne cialis 5mg acheter 200mg gГ©nГ©rique sildenafil en france
tadalafil 20mg kaufen cialis 20mg kaufen ohne rezept viagra kaufen
buy modafinil 200mg for sale Cialis mail order usa diamox order online
order clonidine 0.1mg without prescription buy generic tiotropium bromide buy spiriva 9 mcg online cheap
order buspirone 5mg pills buspar 5mg pills ditropan 5mg ca
order olmesartan 10mg without prescription diamox us cheap acetazolamide 250mg
order tacrolimus 5mg generic tacrolimus drug purchase urso pill
purchase isosorbide generic digoxin for sale online purchase telmisartan
oral bupropion 150 mg order bupropion online cheap purchase seroquel generic
molnupiravir 200 mg uk buy prevacid 30mg pills prevacid pill
tadalafil 5mg oral purchase tadalafil online amantadine 100 mg price
buy provera order cyproheptadine 4 mg sale generic periactin
isotretinoin 40mg over the counter accutane 40mg cost buy prednisone 40mg without prescription
piracetam 800 mg over the counter cheap viagra without prescription viagra 100mg cheap
azithromycin 500mg cost order prednisolone 40mg without prescription cheap gabapentin
order lasix generic plaquenil pills buy plaquenil for sale
purchase itraconazole online buy tinidazole 500mg generic tindamax 500mg without prescription
order metformin 500mg pill buy cialis 40mg tadalafil dosage
purchase zyprexa diovan 160mg without prescription valsartan 80mg sale
sildenafil 50mg cheap sildenafil 100mg drug buy lisinopril 2.5mg
prilosec us purchase omeprazole sale free casino
purchase lopressor online cheap order atenolol 100mg online cheap levitra 20mg drug
buy vardenafil 20mg online cheap order lyrica 75mg pills methylprednisolone 16mg over the counter
custom research paper writing pay for term paper viagra 50mg canada
order clomid 50mg sale order ventolin 4mg poker online free
aristocort 10mg over the counter brand clarinex 5mg purchase clarinex without prescription
dapoxetine 30mg uk priligy order levothyroxine over the counter
buy cialis 40mg pill buy viagra 100mg pills viagra 25mg for sale
xenical cost order diltiazem 180mg generic acyclovir pill
us cialis sales buy clopidogrel 150mg sale cost clopidogrel 150mg
purchase allopurinol pills purchase ezetimibe ezetimibe 10mg price
cozaar 25mg canada buy topamax 100mg sale order topiramate without prescription
ozobax over the counter order ozobax generic toradol pills
order imitrex pill levofloxacin 500mg pills avodart sale
buy colchicine 0.5mg sale roulette casino online poker sites
buy ranitidine 300mg sale order meloxicam 7.5mg sale celebrex 200mg over the counter
play blackjack casino games online poker games online casino blackjack
cheap flomax zofran 4mg generic buy aldactone 25mg without prescription
buy tadalafil 10mg for sale tadalafil 5mg drug buy ciprofloxacin 1000mg pill
cialis coupon walmart oral tadalafil sildenafil online order
order generic ceftin 250mg order robaxin online order methocarbamol pills
san manuel casino online play slots order tadalafil 40mg sale
sildenafil 200mg price buy lamotrigine online purchase lamotrigine sale
poker online for money stromectol 12mg uk provigil cost
buy lasix without prescription furosemide brand order hydroxychloroquine 400mg pill
buy tretinoin cream without prescription tretinoin gel brand buy avanafil 100mg generic
buy tadacip 20mg generic order tadacip 20mg indocin order online
lamisil 250mg tablet purchase amoxicillin for sale amoxicillin 500mg canada
buy biaxin 250mg sale order meclizine generic meclizine drug
buy naproxen 500mg generic buy naproxen order lansoprazole 15mg generic
order spiriva 9mcg online spiriva 9mcg ca buy terazosin generic
buy albuterol online cheap order protonix 20mg online cipro 1000mg cost
female cialis casino free spins no deposit free roulette games
order tadalafil 40mg for sale US cialis order cialis for sale
slot machine no deposit free spins free spins no deposit casino
ivermectin iv buy avlosulfon 100 mg online cheap order dapsone 100mg for sale
blackjack games online casino usa real money online gambling for real money
us blackjack online best college paper writing service help with papers
academic writers online arava 10mg oral sulfasalazine 500 mg cost
order mesalamine 400mg pill generic astelin order avapro 300mg pills
buy clobetasol buspirone 10mg tablet amiodarone 200mg without prescription
acetazolamide 250mg drug cost azathioprine 25mg cost imuran
tricor 160mg us order tricor 160mg pill viagra 100mg england
cialis 40mg cost Cialis order buy ed medication
provigil drug buy generic provigil order generic phenergan
stromectol for sale online stromectol price prednisone 10mg canada
prednisolone online brand furosemide buy furosemide 100mg online