php - 如何在 php 中使用 SMTP 发送电子邮件

标签 php email smtp

我想在我的项目中使用 SMTP 发送电子邮件,之前我在我的项目中编写了 php mail() 但现在我的客户希望我应该使用 SMTP。我对此进行了搜索,但没有找到任何合适的解决方案。

在我的 php mail() 中,我发送姓名、主题和评论,那么我如何在 SMTP 中执行此操作。

这是我的代码:

$payer_email = "Your Email";
$subject = "Your Subject";
$message = 'Dear '.$name.',
            Thank you for your purchase from '.$site_url.'. The details of your purchase are below.
            Transaction ID: '.$txn_id.'
            Item Name: '.$item_name.'
            Payment Amount: '.$payment_amount.'
            Payment Amount: '.$payment_status.'
            Paid to: '.$receiver_email.'
            Thanks and Enjoy!';

$headers .= 'From: ' .$from. "\r\n" .'Reply-To: ' .$from . "\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers  .= "Content-Type: text/html; charset=iso-8859-1 ";

//mail to buyer
mail( $payer_email , $subject, $message, $headers );

请给我一些建议或简单而好的教程。

最佳答案

看看 PHP Mailer:

https://github.com/PHPMailer/PHPMailer

该页面的示例:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$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 TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$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 - 如何在 php 中使用 SMTP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25909348/

相关文章:

php - Ajax 响应中的杂散字符?

java - Spring Batch 电子邮件监听器

java - 如何用 Java 发送电子邮件?

vba - 发送前如何查看发送邮件地址?

java - Apache Camel 邮件: sending over smtps not working

php - ASIHTTPRequest - 将 JSON 发布到 PHP

php - 使用PHP&SQL登录失败?

php - html 和 css 中的选项卡式输入

amazon-web-services - 使用第三方smtp服务器时的AWS EC2电子邮件发送限制

html - 在 Delphi 中发送带附件的电子邮件