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

本文翻译自:How to send an email using PHP?

I am using PHP on a website and I want to add emailing functionality. 我在网站上使用PHP,并且想添加电子邮件功能。

I have WAMPSERVER installed. 我已经安装了WAMPSERVER。

How do I send an email using PHP? 如何使用PHP发送电子邮件?


#1楼

参考:https://stackoom.com/question/MNwn/如何使用PHP发送电子邮件


#2楼

If you are interested in html formatted email, make sure to pass Content-type: text/html; 如果您对html格式的电子邮件感兴趣,请确保传递Content-type: text/html; in the header. 在标题中。 Example: 例:

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

For more details, check php mail function. 有关更多详细信息,请检查php 邮件功能。


#3楼

You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer . 您也可以在https://github.com/PHPMailer/PHPMailer使用PHPMailer类。

It allows you to use the mail function or use an smtp server transparently. 它允许您透明地使用邮件功能或使用smtp服务器。 It also handles HTML based emails and attachments so you don't have to write your own implementation. 它还处理基于HTML的电子邮件和附件,因此您不必编写自己的实现。

The class is stable and it is used by many other projects like Drupal, SugarCRM, Yii, and Joomla! 该类是稳定的,它被许多其他项目(例如Drupal,SugarCRM,Yii和Joomla)使用。

Here is an example from the page above: 这是上一页的示例:

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

#4楼

this is very basic method to send plain text email using mail function. 这是使用邮件功能发送纯文本电子邮件的非常基本的方法。

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

#5楼

You can use a mail web service such as Postmark, Sendgrid etc. 您可以使用邮件网络服务,例如邮戳,Sendgrid等。

Sendgrid vs Postmark vs Amazon SES and other email/SMTP API providers? Sendgrid,Postmark,Amazon SES和其他电子邮件/ SMTP API提供程序?

Edit: I just use the Google Gmail API now. 编辑:我现在只使用Google Gmail API I had trouble sending reminder email to my employer's organization due to strict filters. 由于严格的过滤器,我无法向我的雇主组织发送提醒电子邮件。 But Gmail works as long as you don't spam people. 但是,只要您不向他人发送垃圾邮件,Gmail就可以正常工作。


#6楼

Try this: 尝试这个:

<?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);
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值