SMTP查找主机/用户/通过接收电子邮件

时间:2017-06-07 22:48:53

标签: php phpmailer

我正在尝试使用PHPMailer设置我的电子邮件,以通过我网站上的联系表单接收电子邮件/消息。我无法设置它。提交联系表单会调用此php文件sendemail.php

<?php

require_once('phpmailer/PHPMailerAutoload.php');

$toemails = array();

$toemails[] = array(
                'email' => 'myRealEmail@yahoo.com', // Your Email Address
                'name' => 'My Name' // Your Name
            );

// Form Processing Messages
$message_success = 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = 'your-recaptcha-secret-key'; // Your reCaptcha Secret

$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line
$mail->IsSMTP();
$mail->Host = "mail.yourdomain.com";
$mail->SMTPDebug = 3; 
// $mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Port = 26;
$mail->Username = "yourname@yourdomain.com";
$mail->Password = "yourpassword";


if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    if( $_POST['template-contactform-email'] != '' ) {

        $name = isset( $_POST['template-contactform-name'] ) ? $_POST['template-contactform-name'] : '';
        $email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';
        $phone = isset( $_POST['template-contactform-phone'] ) ? $_POST['template-contactform-phone'] : '';
        $service = isset( $_POST['template-contactform-service'] ) ? $_POST['template-contactform-service'] : '';
        $subject = isset( $_POST['template-contactform-subject'] ) ? $_POST['template-contactform-subject'] : '';
        $message = isset( $_POST['template-contactform-message'] ) ? $_POST['template-contactform-message'] : '';

        $subject = isset($subject) ? $subject : 'New Message From Contact Form';

        $botcheck = $_POST['template-contactform-botcheck'];

        if( $botcheck == '' ) {

            $mail->SetFrom( $email , $name );
            $mail->AddReplyTo( $email , $name );
            foreach( $toemails as $toemail ) {
                $mail->AddAddress( $toemail['email'] , $toemail['name'] );
            }
            $mail->Subject = $subject;

            $name = isset($name) ? "Name: $name<br><br>" : '';
            $email = isset($email) ? "Email: $email<br><br>" : '';
            $phone = isset($phone) ? "Phone: $phone<br><br>" : '';
            $service = isset($service) ? "Service: $service<br><br>" : '';
            $message = isset($message) ? "Message: $message<br><br>" : '';

            $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

            $body = "$name $email $phone $service $message $referrer";

            // Runs only when File Field is present in the Contact Form
            if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {
                $mail->IsHTML(true);
                $mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
            }

            // Runs only when reCaptcha is present in the Contact Form
            if( isset( $_POST['g-recaptcha-response'] ) ) {
                $recaptcha_response = $_POST['g-recaptcha-response'];
                $response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );

                $g_response = json_decode( $response );

                if ( $g_response->success !== true ) {
                    echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
                    die;
                }
            }

            $mail->MsgHTML( $body );
            $sendEmail = $mail->Send();

            if( $sendEmail == true ):
                echo '{ "alert": "success", "message": "' . $message_success . '" }';
            else:
                echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }';
            endif;
        } else {
            echo '{ "alert": "error", "message": "Bot <strong>Detected</strong>.! Clean yourself Botster.!" }';
        }
    } else {
        echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }';
    }
} else {
    echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }';
}

?>

调用此PHPMailerAutoload.php文件:

<?php
/**
 * PHPMailer SPL autoloader.
 * PHP Version 5
 * @package PHPMailer
 * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
 * @author Brent R. Matzelle (original founder)
 * @copyright 2012 - 2014 Marcus Bointon
 * @copyright 2010 - 2012 Jim Jagielski
 * @copyright 2004 - 2009 Andy Prevost
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @note This program is distributed in the hope that it will be useful - WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 */

/**
 * PHPMailer SPL autoloader.
 * @param string $classname The name of the class to load
 */
function PHPMailerAutoload($classname)
{
    //Can't use __DIR__ as it's only in PHP 5.3+
    $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
    if (is_readable($filename)) {
        require $filename;
    }
}

if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
    //SPL autoloading was introduced in PHP 5.1.2
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        spl_autoload_register('PHPMailerAutoload', true, true);
    } else {
        spl_autoload_register('PHPMailerAutoload');
    }
} else {
    /**
     * Fall back to traditional autoload for old PHP versions
     * @param string $classname The name of the class to load
     */
    function __autoload($classname)
    {
        PHPMailerAutoload($classname);
    }
}

正如你在初始的php文件中看到的那样,它正在使用'mail()',我现在切换为SMTP。但是,我从来没有使用过它,并且对如何使用它感到非常困惑。正如您从模板中看到的那样,主持人有:mail.yourdomain.com - 这是我的网站域名,我的电子邮件服务提供商域名吗?接下来它要求输入用户名和密码。我在哪里找到那些?用户/通行证是什么?我只是想为我的常规电子邮件地址设置这个,而不是由cPanel或类似的东西生成的。谢谢你的帮助。

我现在做了一些事情,比如ping yahoo和我的域名服务器来获取地址,我想我知道了吗?但不完全确定哪个是正确的。我知道端口应该像587一样。对于雅虎,我通过控制台ping了他们的服务器或其他什么,所以我将填写以下内容:

Host = "mta7.am0.yahoodns.net"
// Host = "smtp.mail.yahoo.com"
Username = "myYahooEmail@yahoo.com"
Password = "myYahooPassword"

我尝试了上述内容,但仍然得到了这个:

  

由于某些意外错误,无法发送电子邮件。请再试一次   后面。

     

原因:SMTP connect()失败。

同一行的另一个问题是,如果这是您的电子邮件帐户的密码,如果您有2步验证或类似的情况会发生什么?雅虎(我希望发送邮件的电子邮件。我也有GMail,但暂时忽略了这一点)实际上甚至不让我用密码登录。每当有人试图登录我的帐户时,他们都会向我发送带有代码的文本。

编辑: 已通过以下评论中发布的链接试用此代码。我想我可能错了?:

$mail->IsSMTP();
$mail->Host = "smtp.mail.yahoo.com";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
// $mail->SMTPSecure = 'ssl';
$mail->Username = "---@yahoo.com";
$mail->Password = "---";

通过Synchro的评论,我添加了$mail->SMTPSecure = 'tls';,这使得我的联系表单永远不会提交(只是保持旋转,就像它想要发送消息一样)。

0 个答案:

没有答案