这是使用PHP PEAR的一种方法
// Pear Mail Library
require_once "Mail.php";
$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // change to address
$subject = 'Insert subject here'; // subject of mail
$body = "Hello world! this is the content of the email"; //content of mail
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'your@gmail.com', //your gmail account
'password' => 'snip' // your password
));
// Send the mail
$mail = $smtp->send($to, $headers, $body);
//check mail sent or not
if (PEAR::isError($mail)) {
echo '<p>'.$mail->getMessage().'</p>';
} else {
echo '<p>Message successfully sent!</p>';
}
如果您使用Gmail SMTP,请记住在“设置”下的Gmail帐户中启用SMTP
编辑:
如果在debian / ubuntu上找不到Mail.php,则可以使用以下方式安装php-pear
sudo apt install php-pear
然后安装邮件扩展:
sudo pear install mail
sudo pear install Net_SMTP
sudo pear install Auth_SASL
sudo pear install mail_mime
然后,您应该可以通过require_once "Mail.php"
位于此处的其他方式简单地加载它:/usr/share/php/Mail.php
mail()
函数的明智选择。