10个使用PHP发送电子邮件的函数
PHP是一种流行的服务器端脚本语言,用于动态网站开发。其中一个常见的应用场景就是发送电子邮件。无论你是要在网站上用PHP发送注册确认邮件,还是通过电子邮件发送通知或促销信息,PHP都可以很好地完成这项任务。以下是10个使用PHP发送电子邮件的函数。
1. mail()
mail()是PHP内置的函数,用于发送电子邮件。这个函数接受4个参数:收件人地址、邮件主题、邮件内容和可选的邮件头信息。这是一个示例:
$email_to = 'user@example.com';
$subject = 'Subject of Email';
$body = 'Body of the Email';
$headers = 'From: sender@example.com';
mail($email_to, $subject, $body, $headers);
2. PHPMailer
PHPMailer是一个流行的第三方库,用于在PHP中发送电子邮件。它提供了丰富的功能,如支持HTML邮件、附件和SMTP认证等。以下是一个简单的示例:
require_once 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_username@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('sender@example.com', 'Sender');
$mail->addAddress('user@example.com', 'Recipient');
$mail->Subject = 'Subject of Email';
$mail->Body = 'Body of the Email';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: '. $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
3. swiftmailer
swiftmailer是另一个流行的第三方库,用于在PHP中发送电子邮件。它提供类似PHPMailer的功能,例如支持HTML邮件和SMTP认证。以下是一个示例:
require_once 'swiftmailer/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 465, 'ssl')
->setUsername('your_username@example.com')
->setPassword('your_password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject('Subject of Email')
->setFrom(array('sender@example.com'=>'Sender'))
->setTo(array('user@example.com'=>'Recipient'))
->setBody('Body of the Email');
if (!$mailer->send($message)) {
echo 'Message could not be sent.';
} else {
echo 'Message has been sent.';
}
4. mailgun
mailgun是一项云服务,用于发送电子邮件。它提供强大的API,使得在PHP中发送电子邮件变得非常简单。以下是一个示例:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.mailgun.net/v3/YOUR_DOMAIN/messages",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
"from" => "Sender <sender@example.com>",
"to" => "user@example.com",
"subject" => "Subject of Email",
"text" => "Body of the Email"
),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic YOUR_API_KEY"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
5. sendmail
sendmail是一个适用于Linux和Unix的邮件传输代理程序,可用于在PHP中发送电子邮件。以下是一个示例:
$to = 'user@example.com';
$subject = 'Subject of Email';
$message = 'Body of the Email';
$headers = 'From: sender@example.com' . "\r
" .
'Reply-To: sender@example.com' . "\r
" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Message has been sent.';
} else {
echo 'Message could not be sent.';
}
6. postfix
postfix是一个流行的邮件传输代理程序,可用于在PHP中发送电子邮件。要使用postfix,需要在服务器上安装和配置它。以下是一个示例:
$to = 'user@example.com';
$subject = 'Subject of Email';
$message = 'Body of the Email';
$headers = 'From: sender@example.com' . "\r
" .
'Reply-To: sender@example.com' . "\r
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers, '-f sender@example.com');
7. PEAR::Mail
PEAR::Mail是PHP的一个邮件处理库,可用于在PHP中发送电子邮件。以下是一个示例:
require_once "Mail.php";
$to = 'user@example.com';
$subject = 'Subject of Email';
$message = 'Body of the Email';
$headers = array(
'From' => 'sender@example.com',
'Reply-To' => 'sender@example.com',
'X-Mailer' => 'PHP/' . phpversion()
);
$smtp = Mail::factory('smtp', array(
'host' => 'smtp.example.com',
'auth' => true,
'username' => 'your_username@example.com',
'password' => 'your_password',
'port' => 465,
'ssl' => 'ssl'
));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo "Message could not be sent.";
echo $mail->getMessage();
} else {
echo "Message has been sent.";
}
8. Zend\Mail
Zend\Mail是Zend Framework的一个组件,可用于在PHP中发送电子邮件。以下是一个示例:
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$to = 'user@example.com';
$subject = 'Subject of Email';
$message = 'Body of the Email';
$mail = new Zend\Mail\Message();
$mail->setSubject($subject);
$mail->setFrom('sender@example.com', 'Sender');
$mail->addTo($to, 'Recipient');
$mail->setBody($message);
$transport = new Zend\Mail\Transport\Smtp();
$options = new Zend\Mail\Transport\SmtpOptions(array(
'host' => 'smtp.example.com',
'connection_class' => 'login',
'connection_config' => array(
'username' => 'your_username@example.com',
'password' => 'your_password',
'ssl' => 'ssl',
'port' => 465
),
));
$transport->setOptions($options);
$transport->send($mail);
9. Mailjet
Mailjet是一项云服务,用于发送电子邮件。它提供强大的API,使得在PHP中发送电子邮件变得非常简单。以下是一个示例:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.mailjet.com/v3.1/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{ "Messages":[ { "From": { "Email": "sender@example.com", "Name": "Sender" }, "To": [ { "Email": "user@example.com", "Name": "Recipient" } ], "Subject": "Subject of Email", "TextPart": "Body of the Email" } ] }',
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Basic YOUR_API_KEY"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
10. Mandrill
Mandrill是一项邮件服务,可用于发送电子邮件。它提供强大的API,使得在PHP中发送电子邮件变得非常简单。以下是一个示例:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://mandrillapp.com/api/1.0/messages/send.json",
CURLOPT_RETURNTRANSFER => true
