我在一个网站上使用PHP,我想添加电子邮件功能。
我安装了WampServer。
如何使用PHP发送电子邮件?
我在一个网站上使用PHP,我想添加电子邮件功能。
我安装了WampServer。
如何使用PHP发送电子邮件?
当前回答
我在cPanel中使用了这个方法,一切工作正常:
<?php
$dest = "destination@domain.tld";
$fromaddy = "cpaneluser@domain.tld";
mail("<$dest>","Test from php mail","Test","From:<$fromaddy>","-f$fromaddy");
?>
其他回答
我在cPanel中使用了这个方法,一切工作正常:
<?php
$dest = "destination@domain.tld";
$fromaddy = "cpaneluser@domain.tld";
mail("<$dest>","Test from php mail","Test","From:<$fromaddy>","-f$fromaddy");
?>
对于未来的读者:如果其他答案都不管用(就像我的情况一样),试试这个:
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;
}
?>
这是非常基本的方法发送纯文本电子邮件使用邮件功能。
<?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);
你也可以在https://github.com/PHPMailer/PHPMailer上使用PHPMailer类。
它允许您使用邮件功能或透明地使用smtp服务器。它还可以处理基于HTML的电子邮件和附件,因此您不必编写自己的实现。
这个类是稳定的,它被许多其他项目使用,如Drupal、SugarCRM、Yii和Joomla!
下面是上面页面的一个例子:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
完整的代码示例..
试一次…
<?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));
?>