【问题标题】:How can i send an email using php including mail.php file我如何使用 php 发送电子邮件,包括 mail.php 文件
【发布时间】:2014-07-26 13:07:12
【问题描述】:

我想从本地主机发送一封包含 mail.php 文件的 php 电子邮件。

这是我的 test.php 文件代码:

<?php 
include_once("Mail.php"); 

$From = "Sender's name <testing123xyz@gmail.com>"; 
$To = "Recipient's name <test2@gmail.com>"; 
$Subject = "Send Email using SMTP authentication"; 
$Message = "This example demonstrates how you can send email with PHP using SMTP authentication"; 

$Host = "mail.gmail.com"; 
$Username = "testing123xyz"; 
$Password = "testing"; 

// Do not change bellow 

$Headers = array ('From' => $From, 'To' => $To, 'Subject' => $Subject); 
$SMTP = Mail::factory('smtp', array ('host' => $Host, 'auth' => true, 
'username' => $Username, 'password' => $Password)); 

$mail = $SMTP->send($To, $Headers, $Message); 

if (PEAR::isError($mail)){ 
echo($mail->getMessage()); 
} else { 
echo("Email Message sent!"); 
} 
?>

当我点击 test.php 文件的 url 时,服务器抛出以下错误:

无法连接到 smtp.gmail.com:25 [SMTP:无法连接套接字:无法建立连接,因为目标机器主动拒绝了它。 (代码:-1,响应:)]

请帮帮我。 谢谢。

【问题讨论】:

    标签: php email xampp


    【解决方案1】:

    我的猜测是您使用的是本地主机。尝试使用Test Mail Server

    编辑截至link,您使用了错误的主机。改成smtp.gmail.com

    【讨论】:

    • 安装测试邮件服务器工具时仍然出现同样的错误。 :(
    • @user3454311 你确定你传递了正确的 SMTP 登录信息吗?
    • 是的,我已经输入了我很久以来使用的 gmail id 和密码。
    【解决方案2】:

    从github下载使用phpmailer:https://github.com/PHPMailer/PHPMailer 另外,请查看本教程,它将对您有所帮助:http://phpmailer.worxware.com/?pg=tutorial

    这是发送电子邮件的 php 代码:

     <?php
    
            function email($recipient_email_id,$senders_name){
    
            include("phpmailer/class.phpmailer.php");
    
                $mail = new PHPMailer();
                $mail->IsSMTP(); // send via SMTP
                $mail = new PHPMailer();
                $mail->IsSMTP(); // send via SMTP
            //IsSMTP(); // send via SMTP
                $mail->SMTPAuth = true; // turn on SMTP authentication
                $mail->Username = "sender@gmail.com"; // SMTP username
                $mail->Password = "xxx"; // SMTP password
                $webmaster_email = "reply@gmail.com"; //Reply to this email ID
                $email = "$recipient_email_id"; // Recipients email ID
                $name = "$senders_name"; // sender's's name
                $mail->From = $webmaster_email;
                $mail->FromName = $name;
             $mail->AddAddress($email, $name);
                $mail->AddReplyTo($webmaster_email, "Name");
                $mail->WordWrap = 50; // set word wrap
            $mail->IsHTML(true); // send as HTML
                $mail->Subject = "This is the subject";
                $mail->Body = "Hi,
            This is the HTML BODY "; //HTML Body
                $mail->AltBody = "This is the body when user views in plain text format"; //Text Body
                if (!$mail->Send()) {
                    echo "Mailer Error: " . $mail->ErrorInfo;
                } else {
                    echo "Message has been sent";
                }
            }
            ?>
    

    【讨论】:

    • 我使用这个并且能够使用本地主机发送邮件
    • 收到此错误:邮件未发送。邮件程序错误:SMTP 连接()失败。
    • 收到消息为:消息已发送。但没有收到相应电子邮件 ID 的电子邮件(即使在垃圾邮件中也没有)。 :(
    • email($recipient_email_id,$senders_name),你把邮箱和名字传到这里了吗??另外,这是一个函数,所以你可能想调用它,或者直接调用内容而不包含在函数中
    猜你喜欢
    • 2012-07-03
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多