如何发送带有PHP代码的电子邮件?

时间:2016-03-30 12:31:27

标签: php codeigniter email

我正在尝试从我的codeigniter项目中发送电子邮件,并且命令 echo $this->email->print_debugger(); 一直返回以下内容:

  

220 smtp.gmail.com ESMTP q139sm4099547wmd.2 - gsmtp
  你好:250-smtp.gmail.com为你服务,[41.​​228.226.174]   250-SIZE 35882577   250-8BITMIME   250-STARTTLS   250 ENHANCEDSTATUSCODES   250流水   250 SMTPUTF8   无法发送AUTH LOGIN命令。错误:530 5.7.0必须   首先发出STARTTLS命令。 q139sm4099547wmd.2 - gsmtp无法使用   使用PHP SMTP发送电子邮件。您的服务器可能未配置为发送   邮件使用这种方法。

所以我回去试图用一个简单的PHP代码发送电子邮件,没有codeigniter,结果发现最初的问题与代码服务器无关,因为我再次失败了。 这是我的PHP代码:

<?php
$to       = '**********@gmail.com';
$subject  = 'Testing sendmail.exe';
$message  = 'Hi, you just received an email using sendmail!';
$headers  = 'From: ******@gmail.com' . "\r\n" .
            'MIME-Version: 1.0' . "\r\n" .
            'Content-type: text/html; charset=utf-8';
if(mail($to, $subject, $message, $headers))
    echo "Email sent";
else
    echo "Email sending failed";
?>

这里是php.ini中的邮件配置,对于记录,我尝试了以下函数,其中smtp_port = 25行和sendmail_from = ********@gmail.com注释(带有初始“;”)并且没有“ ;”因为我在尝试解决问题时在论坛中阅读了这个建议。

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = ********@gmail.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path="C:\wamp\sendmail\sendmail.exe -t -i"

我也尝试删除行sendmail_path=.....

最后这是我的sendmail.ini:

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.gmail.com

; smtp port (normally 25)

smtp_port=25

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=

; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify

default_domain=

; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging

error_logfile=error.log

; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging

debug_logfile=debug.log

; if your smtp server requires authentication, modify the following two lines

auth_username=***********@gmail.com
auth_password=*******************

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines.  do not enable unless it is required.

pop3_server=
pop3_username=
pop3_password=

; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify 
; the "From: " header of the message content

force_sender=************@gmail.com

; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify 
; the "To: " header of the message content

force_recipient=

; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting.  you can manually set the ehlo/helo name if required

hostname=localhost

任何帮助都会受到高度赞赏,因为我花了3天时间试图解决这个问题无济于事。

编辑:第二个代码确实返回“已发送电子邮件”,但即使在垃圾邮件中也无法找到它。是的,我已经将gmail配置为“允许不太安全的应用程序”

3 个答案:

答案 0 :(得分:0)

您的电子邮件发送部分是正确的。没问题。

如果您要发送邮件,则必须正确配置电子邮件设置。

  1. 如果 XAMPP Read this
  2. 如果 Wamp Read This
  3.   

    注意:如果您有实时服务器,请将其上传到实时。它比配置本地设置和所有设置更容易检查。

答案 1 :(得分:0)

您的电子邮件代码没问题,但如果您使用的是wamp服务器.....需要更改以下内容...

  1. 访问您的电子邮件帐户。单击Gear Tool&gt;设置&gt; 转发和POP / IMAP&gt; IMAP访问。单击&#34;启用IMAP&#34;,然后 保存更改
  2. 运行您的WAMP服务器。在Apache Module下启用ssl_module。
  3. 接下来,在PHP下启用php_openssl和php_sockets。
  4. **在"C:\wamp\bin\php\php5.5.12\php.ini"上设置 sendmail_path php.ini 文件 "C:\wamp\bin\apache\apache2.4.9\bin\php.ini" **
  5.   

    sendmail_path =&#34; C:\ wamp \ sendmail \ sendmail.exe -t&#34;

    1. 重启Wamp Server。
    2. 我认为它会奏效。

答案 2 :(得分:0)

在config / email.php中,我不得不添加

$ config ['smtp_crypto'] =“tls”;