我在一个网站上使用PHP,我想添加电子邮件功能。

我安装了WampServer。

如何使用PHP发送电子邮件?


当前回答

完整的代码示例..

试一次…

<?php
// Multiple recipients
$to = 'johny@example.com, sally@example.com'; // note the comma

// 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>Johny</td><td>10th</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';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers
$headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';

// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
?>

其他回答

试试这个:

<?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);
?>

我尝试在我的快速时间,我有同样的问题,但经过适当的研究,我解决了它。以下是我的方法。您必须下载PHPMailer源文件,并在项目中手动包含所需的文件。

您可以从PHPMailer主页1下载带有源代码的ZIP文件,点击“克隆或下载”绿色按钮(在右侧),然后选择“下载ZIP”。在要保存源文件的目录中解压缩包。

1来自:GitHub。 第二步:打开“(来自Gmail地址)谷歌帐户”,执行以下步骤:

禁用谷歌帐号的双因素密码验证。

Turn on Less Security. Allow third party app. There you Go.. <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; session_start(); if (isset($_POST['send'])) { $email = $_POST['email']; $subject = $_POST['subject']; $message = "I am trying"; //Load composer's autoloader $mail = new PHPMailer(true); try { //Server settings $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'youremail@gmail.com'; $mail->Password = 'password'; $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); $mail->SMTPSecure = 'ssl'; $mail->Port = 465; //Send Email $mail->setFrom('youremail@gmail.com'); //Recipients $mail->addAddress($email); $mail->addReplyTo('youremail@gmail.com'); //Content $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $message; $mail->send(); $_SESSION['result'] = 'Message has been sent'; $_SESSION['status'] = 'ok'; } catch (Exception $e) { $_SESSION['result'] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo; $_SESSION['status'] = 'error'; echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo; } } header("location: forgotPassword.php");

这是非常基本的方法发送纯文本电子邮件使用邮件功能。

<?php
$to = 'SomeOtherEmailAddress@Domain.com';
$subject = 'This is subject';
$message = 'This is body of email';
$from = "From: FirstName LastName <SomeEmailAddress@Domain.com>";
mail($to,$subject,$message,$from);

对于未来的读者:如果其他答案都不管用(就像我的情况一样),试试这个:

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;
}
?>

如果你对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邮件函数。