如何在 php 中使用 SMTP 发送电子邮件
Posted
技术标签:
【中文标题】如何在 php 中使用 SMTP 发送电子邮件【英文标题】:How to send email with SMTP in php 【发布时间】:2014-11-12 14:21:23 【问题描述】:我想在我的项目中使用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 );
请给我一些建议或简单而好的教程。
【问题讨论】:
这能回答你的问题吗? Sending email with PHP from an SMTP server 【参考方案1】:看看 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';
【讨论】:
请告诉我,我得到“PHPmailer”文件夹并放入我的项目并编写此代码。 你应该下载PHPMailer,把它放到你的项目目录中,并像require 'path/to/PHPMailer/PHPMailerAutoload.php;
一样将它包含在你的项目中,然后如上所示使用它
抱歉耽搁了......这是在“localhost”上工作还是只在在线服务器上工作......因为我在localhost上测试它但它没有工作
它也应该在localhost上工作,这个类实现了SMTP协议并连接到您的邮件服务器以发送电子邮件。
如果您使用的是 WP,您可以在脚本中包含内置类 wp-includes/class-phpmailer.php
【参考方案2】:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
【讨论】:
【参考方案3】:请试试这个
为 SMTP 设置“library.php”创建库文件:
<?php
error_reporting(0);
define("SMTP_HOST", "SMTP_HOST_NAME"); //Hostname of the mail server
define("SMTP_PORT", "SMTP_PORT"); //Port of the SMTP like to be 25, 80, 465 or 587
define("SMTP_UNAME", "VALID_EMAIL_ACCOUNT"); //Username for SMTP authentication any valid email created in your domain
define("SMTP_PWORD", "VALID_EMAIL_ACCOUNTS_PASSWORD"); //Password for SMTP authentication
?>
Make the form post and do the below actions:
<?php
include 'library.php';
include "classes/class.phpmailer.php"; // include the class file name
if(isset($_POST["send"]))
$email = $_POST["email"];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->AddReplyTo("reply@yourdomain.com", "Reply name"); //reply-to address
$mail->SetFrom("from@yourdomain.com", "Asif18 SMTP Mailer"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>"); //Put your body of the message you can place html code here
$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
if($send)
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
else
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
?>
请正确编辑您的电子邮件和密码。
您可以在Click here看到演示和源代码
【讨论】:
演示链接已损坏 没有第三方脚本的代码怎么样?我发现的一切都在使用 PHPMailer 或其他一些我不能/不想使用的脚本。好吧,我可以对 PHPMailer 进行逆向工程,并尝试仅获取通过 SMTP 而不是 PHP 邮件发送所需的内容。以上是关于如何在 php 中使用 SMTP 发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
通过 Microsoft 365 SMTP 发送电子邮件时如何使用自定义显示名称? (以 PHP Swift Mailer 为例)