我在一个网站上使用PHP,我想添加电子邮件功能。
我安装了WampServer。
如何使用PHP发送电子邮件?
我在一个网站上使用PHP,我想添加电子邮件功能。
我安装了WampServer。
如何使用PHP发送电子邮件?
当前回答
试试这个:
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
其他回答
如果你对html格式的电子邮件感兴趣,请确保传递Content-type: text/html;在头文件中。例子:
// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
要了解更多细节,请查看php邮件函数。
试试这个:
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
对于未来的读者:如果其他答案都不管用(就像我的情况一样),试试这个:
1)。下载PHPMailer,打开zip文件并解压到您的项目目录。
3)。将提取的目录重命名为phpailer,并在php脚本中编写以下代码(脚本必须在phpailer文件夹之外)
<?php
// PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Base files
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
// create object of PHPMailer class with boolean parameter which sets/unsets exception.
$mail = new PHPMailer(true);
try {
$mail->isSMTP(); // using SMTP protocol
$mail->Host = 'smtp.gmail.com'; // SMTP host as gmail
$mail->SMTPAuth = true; // enable smtp authentication
$mail->Username = 'sender@gmail.com'; // sender gmail host
$mail->Password = 'password'; // sender gmail host password
$mail->SMTPSecure = 'tls'; // for encrypted connection
$mail->Port = 587; // port for SMTP
$mail->setFrom('sender@gmail.com', "Sender"); // sender's email and name
$mail->addAddress('receiver@gmail.com', "Receiver"); // receiver's email and name
$mail->Subject = 'Test subject';
$mail->Body = 'Test body';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) { // handle error.
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
还可以查看PEAR邮件包PEAR mail Page
它似乎比内置的标准mail()函数健壮一些(如果标准函数不够的话)。
下面是该页面的一段摘录,展示如何使用它。PEAR邮件发送()用法
<?php
include('Mail.php');
$recipients = 'joe@example.com';
$headers['From'] = 'richard@example.com';
$headers['To'] = 'joe@example.com';
$headers['Subject'] = 'Test message';
$body = 'Test message';
$smtpinfo["host"] = "smtp.server.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtp_user";
$smtpinfo["password"] = "smtp_password";
// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $smtpinfo);
$mail_object->send($recipients, $headers, $body);
?>
从PHP发送电子邮件的核心方法是使用其内置的mail()函数,但有几个现成的sdk可以简化集成:
Swiftmailer PHPMailer peppost(通过HTTP工作,因此可以避免SMTP端口阻塞问题) Sendmail
附注:我受雇于peppost公司。