返回顶部

收藏

PHP 使用 SMTP 发送邮件(PEAR)

更多

PHPer 多数使用 mail 函数来发送邮件,但我们可以使用其他的 SMTP 服务器来发送,这里推荐使用PEAR’s mail package来发送邮件。

[PHP]代码

$subject = "This mail is sent from SMTP.";
$mail_body = "This is the body of the mail which is sent using SMTP.";
$from = "From: From Name <fromaddress@xpertdeveloper.com>"; 
$to = "To: To Name <toaddress@xpertdeveloper.com>"; 
$receiver = "toaddress@xpertdeveloper.com"; 

// Setting up the headers
$headers["From"] = $from; 
$headers["To"] = $to; 
$headers["Subject"] = $subject; 
$headers["Reply-To"] = "reply@address.com"; 
$headers["Content-Type"] = "text/plain; charset=ISO-2022-JP"; 
$headers["Return-path"] = "returnpath@address.com"; 

// Setting up the SMTP setting
$smtp_info["host"] = "smtp.server.com"; 
$smtp_info["port"] = "25"; 
$smtp_info["auth"] = true; 
$smtp_info["username"] = "smtp_user"; 
$smtp_info["password"] = "smtp_password"; 

// Creating the PEAR mail object :
$mail_obj =& Mail::factory("smtp", $smtp_info); 

// Sending the mail now
$mail_sent = $mail_obj->send($receiver, $headers, $mail_body); 

// If any error the see for that here:
if (PEAR::isError($mail_sent)) { print($mail_sent->getMessage());}

标签:SMTP

收藏

0人收藏

支持

0

反对

0