php 如何更正使用codeigniter发送电子邮件的方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6195709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:38:04  来源:igfitidea点击:

how to correct way to send email using codeigniter

phpemailcodeignitersmtp

提问by user778655

Hey guy, I'm trying to send email using codeiginiter mail class function, but I found a trouble with smtp protocol. I using gmail smtp protocol. I running this in local machine. I'm using Xampp 1.7.4 package, andI have tried setting like below :

嘿家伙,我正在尝试使用 codeiginiter 邮件类功能发送电子邮件,但我发现 smtp 协议有问题。我使用 gmail smtp 协议。我在本地机器上运行它。我正在使用 Xampp 1.7.4 包,并且我尝试过如下设置:

function index()
{

    $config['protocol'] = 'smtp'; // mail, sendmail, or smtp    The mail sending protocol.
    $config['smtp_host'] = 'smtp.gmail.com'; // SMTP Server Address.
    $config['smtp_user'] = '[email protected]'; // SMTP Username.
    $config['smtp_pass'] = '123'; // SMTP Password.
    $config['smtp_port'] = '25'; // SMTP Port.
    $config['smtp_timeout'] = '5'; // SMTP Timeout (in seconds).
    $config['wordwrap'] = TRUE; // TRUE or FALSE (boolean)    Enable word-wrap.
    $config['wrapchars'] = 76; // Character count to wrap at.
    $config['mailtype'] = 'html'; // text or html Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
    $config['charset'] = 'utf-8'; // Character set (utf-8, iso-8859-1, etc.).
    $config['validate'] = FALSE; // TRUE or FALSE (boolean)    Whether to validate the email address.
    $config['priority'] = 3; // 1, 2, 3, 4, 5    Email Priority. 1 = highest. 5 = lowest. 3 = normal.
    $config['crlf'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
    $config['newline'] = "\r\n"; // "\r\n" or "\n" or "\r"    Newline character. (Use "\r\n" to comply with RFC 822).
    $config['bcc_batch_mode'] = FALSE; // TRUE or FALSE (boolean)    Enable BCC Batch Mode.
    $config['bcc_batch_size'] = 200; // Number of emails in each BCC batch.

    $this->load->library('email');
    $this->email->initialize($config);


    $this->email->from('[email protected]', 'Me');
    $this->email->reply_to('[email protected]', 'Me');
    $this->email->to('[email protected]');
    $this->email->subject('testing my mail function with CodeIgniter');
    $this->email->message('<html><body>this is the content</body></html>');

    if ( ! $this->email->send()){
        echo 'error! <br />';
        // Generate error
    }
    echo $this->email->print_debugger();

}

and below error was show in my browser :

我的浏览器中显示以下错误:

error!
220 mx.google.com ESMTP b8sm581192pbj.46

hello: 250-mx.google.com at your service, [118.96.231.25]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES

Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46

from: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46

to: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46

data: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. b8sm581192pbj.46
502 5.5.1 Unrecognized command. b8sm581192pbj.46
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. b8sm581192pbj.46
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

User-Agent: CodeIgniter
Date: Wed, 1 Jun 2011 09:27:21 +0700
From: "Me" 
Return-Path: 
Reply-To: "Me" 
To: [email protected]
Subject: =?utf-8?Q?testing_my_mail_function_with_CodeIgniter?=
X-Sender: [email protected]
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_4de5a38938733"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_4de5a38938733
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

this is the content


--B_ALT_4de5a38938733
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html><body>this is the content</body></html>

--B_ALT_4de5a38938733--

回答by King Skippus

For security reasons, Google's e-mail servers require SSL or TLSin order to send mail. It doesn't use the standard unencrypted port 25 method of doing it.

出于安全原因,Google 的电子邮件服务器需要 SSL 或 TLS才能发送邮件。它不使用标准的未加密端口 25 方法来执行此操作。

You basically have two choices. 1) Use sendmail or mail on your own server to deliver the messages, or 2) use the method described hereto send e-mail via Google's servers using SSL. Good luck, and I hope it this helps.

你基本上有两个选择。1) 使用 sendmail 或您自己服务器上的邮件发送邮件,或 2) 使用此处描述的方法使用 SSL 通过 Google 的服务器发送电子邮件。祝你好运,我希望这会有所帮助。

回答by Michael Berkowski

You are attempting to send outbound mail via Gmail on standard SMTP port 25. Gmail does not accept unencrypted connections on that port (or any port). You must use port 465over SSL.

您正尝试在标准 SMTP 端口 25 上通过 Gmail 发送出站邮件。Gmail 不接受该端口(或任何端口)上的未加密连接。您必须使用465SSL 上的端口。

You must change your $config['smtp_port']to 465and your $config['smtp_host']to 'ssl://smtp.googlemail.com'

你必须改变你$config['smtp_port']465和你$config['smtp_host']'ssl://smtp.googlemail.com'

More info here

更多信息在这里

回答by Sahil Jariwala

You can even use port 587:

您甚至可以使用端口 587:

$config['smtp_port'] = '587';

and smtp-host as:

和 smtp-host 为:

$config['smtp_host'] = 'ssl://smtp.gmail.com';

回答by Enver Vivas

I resolved this issue by including the sslprefix on smtp_hostleaving it this way:

我通过包含ssl前缀来解决这个问题smtp_host

$config['smtp_host'] = 'ssl://smtp.gmail.com';

And be sure to use the 465 port (that's google's smtp port):

并确保使用 465 端口(即 google 的 smtp 端口):

$config['smtp_port'] = '465';

Otherwise, by default google blocks the 'low security conections', so you can enter to your google account and configure this option.

否则,默认情况下 google 会阻止“低安全性连接”,因此您可以输入您的 google 帐户并配置此选项。

Allow less secure apps to access accounts

允许安全性较低的应用访问帐户

Activate this option and then try again. Hope this can help you!.

激活此选项,然后重试。希望这可以帮到你!。

回答by CHORTik

1) You smtp port must be 465.

1) 您的 smtp 端口必须是465.

2) You smtp host must be ssl://smtp.gmail.com.

2) 您的 smtp 主机必须是ssl://smtp.gmail.com.

$config['smtp_host'] = 'ssl://smtp.gmail.com'; // SMTP Server Address.

$config['smtp_port'] = '465'; // SMTP Port.