使用smtp配置php邮件

时间:2016-08-17 11:04:53

标签: php email smtp phpmailer smtp-auth

我有没有smtp aunthetication的php邮件,邮件服务器不允许我不使用smtp发送电子邮件。我尝试了很多选择,但我无法修复它;我是php世界的新手。请帮我修理一下。 这是我的php邮件代码:

<?php

/**
 * Sends an e-mail based on a template
 *
 * @param $template_name
 * @param $template_data
 * @param $recipient_mail
 * @param $subject
 * @param bool $is_admin
 * @throws Exception
 * @throws SmartyException
 */
function send_email_template($template_name, $template_data, $recipient_mail, $subject, $is_admin = false){

    global $template;

    $template->assign('mail', $template_data);

    if($is_admin) {
        $body = $template->fetch(ADMIN_TEMPLATE . 'email/' . $template_name);
    }else{
        $body = $template->fetch(CURRENT_TEMPLATE . 'email/' . $template_name);
    }

    $headers = sprintf('From: %s', get_setting('email_from')) . '\r\n';
    $headers  .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    mail($recipient_mail, $subject, $body, $headers);

}

/**
 * Checks if email exists based on given $email
 *
 * @param $email
 * @return bool
 */
function email_exists($email){

    $sql = 'select * from members where members_email = ' . make_safe($email);
    return fetch_row($sql);

}

/**
 * Updates the email code for a member based on their email
 *
 * @param $email
 * @param $email_code
 * @return bool
 */
function update_email_code($email, $email_code){

    global $database;

    try {

        $rs = $database->Execute('select * from members where members_email = ' . make_safe($email));

        $member = array();

        $member['members_email_code'] = $email_code;

        $updateSQL = $database->GetUpdateSQL($rs, $member);
        $database->Execute($updateSQL);

        return true;

    } catch (exception $e) {

        error_log($e);

        return false;

    }

}

2 个答案:

答案 0 :(得分:2)

使用此脚本使用SMTP发送电子邮件。 在这种情况下,我们使用的是Google SMTP服务器,请使用此链接了解Google SMTP https://support.google.com/a/answer/176600?hl=en

<?php
    ini_set("SMTP", "aspmx.l.google.com");
    ini_set("sendmail_from", "YOURMAIL@gmail.com");

    $message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail@address.com";

    $headers = "From: YOURMAIL@gmail.com";


    mail("Sending@provider.com", "Testing", $message, $headers);
    echo "Check your email now....<BR/>";
?>

答案 1 :(得分:0)

下面的代码适用于我

require_once('../class.phpmailer.php');

//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

如果您使用Gmail作为SMTP

转到您的帐户设置 - &gt; https://myaccount.google.com/security?utm_source=OGB&pli=1#connectedapps

点击&#34;允许安全性较低的应用&#34;到ON。