在phpmailer中使用smtp gmail发送电子邮件

时间:2016-05-25 10:07:17

标签: php smtp gmail phpmailer

我尝试使用phpmailer使用smtp gmail发送电子邮件。我的Gmail帐户被暂停,说有一个不寻常的活动。这是我用来发送电子邮件的代码。在phpmailer中使用smtp gmail发送电子邮件的正确方法是什么?

我的问题不重复。我已经尝试过这个:Send email using the GMail SMTP server from a PHP page

我正在使用phpmailer,我的帐户被暂停。

<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
      // Set default variables for all new objects
    public $From     = 'noreply@exmaple.org';
    public $FromName = SITETITLE;
    public $Host     = 'smtp.gmail.com';
    public $Mailer   = 'smtp';
    public $SMTPAuth = true;
    public $Username = 'username@gmail.com';
    public $Password = 'password';
    public $SMTPSecure = 'tls';
    public $WordWrap = 75;

    public function subject($subject)
    {
        $this->Subject = $subject;
    }

    public function body($body)
    {
        $this->Body = $body;
    }

    public function send()
    {
        $this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
        $this->AltBody = str_replace("&nbsp;", "\n\n", $this->AltBody);
        return parent::send();
    }
}

这是我的php代码:

$to = $_POST['email'];
            $subject = "Registration Confirmation";
            $body = "<p>Thank you for registering at demo site.</p>
            <p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
            <p>Regards Site Admin</p>";

            $mail = new Mail();
            $mail->setFrom('noreply@example.org');
            $mail->addAddress($to);
            $mail->subject($subject);
            $mail->body($body);
            $mail->send();

            //redirect to index page
            header('Location: index.php?action=joined');
            exit;

0 个答案:

没有答案