PHP邮件发送的10个重要函数
1. mail()
mail()函数是PHP中最常用的邮件发送函数,可以使用SMTP发送邮件,也可以使用sendmail发送邮件。其基本语法如下:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
该函数发送邮件给to指定的收件人,邮件主题为subject,正文为message。有时需要在邮件的头部添加一些附加的信息,比如发件人的email、时间、优先级等,这时可以使用第四个参数additional_headers。有时要带上一些参数,比如SMTP服务器地址和端口号,这时可以使用第五个参数additional_parameters。
2. smtpmail()
smtpmail()函数用于基于SMTP协议发送邮件,并且需要提供SMTP服务器的地址和认证信息。该函数需要PHP安装了SMTP扩展,基本语法如下:
bool smtpmail ( string $to , string $subject , string $message [, string $headers [, string $parameters ]] )
该函数的参数和mail()函数类似,额外需要提供SMTP服务器的地址,用户名和密码,可以使用参数$headers和$parameters指定。
3. imap_mail()
imap_mail()函数用于发送邮件并将邮件存储在IMAP服务器的发件箱中。其基本语法如下:
bool imap_mail ( string $to , string $subject , string $message [, string $additional_headers [, string $cc [, string $bcc [, string $rpath ]]]] )
该函数和mail()函数类似,能够发送指定的邮件,同时可以指定收件人、抄送和密送地址。还可以指定一些附加的头部信息以及邮件保存在IMAP服务器上的路径。
4. imap_send()
imap_send()函数也是将邮件保存在IMAP服务器上的,不过这个函数是通过IMAP协议发送邮件,而不是使用SMTP协议。其基本语法如下:
bool imap_send ( resource $imap_stream , string $from , string $to , string $message [, string $options = NULL [, string $cc = NULL [, string $bcc = NULL [, string $rpath = NULL ]]]] )
该函数的 个参数是通过imap_open()函数创建的IMAP连接,接下来的参数和imap_mail()函数的类似。
5. mb_send_mail()
mb_send_mail()函数发送邮件和mail()函数类似,不过该函数支持多Byte字符集,可以输入中文和其他非拉丁字符。其基本语法如下:
bool mb_send_mail ( string $to , string $subject , string $message [, string $additional_headers = NULL [, string $additional_parameter = NULL ]] )
该函数和mail()函数很像,只不过使用了多Byte字符集,同时也支持SMTP和sendmail协议。
6. fsockopen()
fsockopen()函数可以连接到指定的网络套接字,并发送和接收数据。可以用来实现SMTP发邮件。其基本语法如下:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
该函数的 个参数指定远程请求的主机名和端口号,其余参数为可选参数。
7. socket_create()
socket_create()函数创建一个套接字连接,可以实现SMTP协议发送邮件和接收邮件。其基本语法如下:
resource socket_create ( int $domain , int $type , int $protocol )
该函数的 个参数指定协议族,第二个参数指定套接字类型,第三个参数指定协议类型。
8. socket_send()
socket_send()函数发送信息到套接字连接中,通常和socket_create()函数一起使用。可以用来发送SMTP邮件。其基本语法如下:
int socket_send ( resource $socket , string $buffer , int $length [, int $flags = 0 ] )
该函数参数指定连接的套接字、要发送的数据和数据的字节数。
9. Pear::Mail()
PEAR是PHP Extension and Application Repository的缩写,是一个类库和应用程序的存储库。Pear::Mail()函数是PEAR类库中的发送邮件的函数。其基本语法如下:
require_once "Mail.php";
$mailer = Mail::factory("smtp",
array("host" => "smtp.gmail.com",
"port" => "465",
"auth" => true,
"username" => "user@gmail.com",
"password" => "password"));
$headers = array(
"From" => "user@example.com",
"To" => "recipient@example.com",
"Subject" => "Test email",
"Content-Type" => "text/html");
$body = "This is a test email.<br><br>";
$mail = $mailer->send("recipient@example.com", $headers, $body);
以上代码使用PEAR类库里的Mail::factory()函数连接到SMTP服务器,并发送邮件。
10. PHPMailer
PHPMailer是PHP中非常流行的第三方邮件发送类库,比较易用而且功能强大。其基本语法如下:
require './PHPMailer/PHPMailer.php';
require './PHPMailer/SMTP.php';
require './PHPMailer/Exception.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('user@example.com', 'Mailer');
$mail->addAddress('recipient@example.com');
$mail->addReplyTo('user@example.com', 'Information');
$mail->isHTML(true);
$mail->Subject = 'Test email';
$mail->Body = 'This is a test email.<br><br>';
$mail->send();
$mail->clearAddresses();
$mail->clearAttachments();
以上代码使用了PHPMailer类库,可以基于SMTP协议发送邮件,并提供了发送邮件时需要的各种选项。
