简体   繁体   中英

How to send an email with php code?

I was trying to send an email from within my codeigniter project, and the command echo $this->email->print_debugger(); kept returning the following:

220 smtp.gmail.com ESMTP q139sm4099547wmd.2 - gsmtp
hello: 250-smtp.gmail.com at your service, [41.228.226.174] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250 SMTPUTF8 Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. q139sm4099547wmd.2 - gsmtp Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

so I went back and tried to send email with a simple php code without codeigniter and turns out that the initial problem had nothing to do with codeingiter since I failed again. here is my php code:

<?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";
?>

and here is the mail configuration in php.ini and for the record, I tried the following function with the lines smtp_port = 25 and sendmail_from = ********@gmail.com commented (with the initial ";") and without the ";" because I read that suggestion in a forum while trying to solve my problem.

[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"

I also tried removing the line sendmail_path=.....

and finally here is my 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

Any help would be highly appreciated since I have spent over 3 days trying to solve this problem to no avail.

edit: the second code indeed returns "Email sent" but I can't find it anywhere not even in the spam. and yes I have configured gmail to" allow less secure apps"

Your E-Mail sending part is correct. No issue with it.

If you are sending mail through you have to configure email settings correctly.

  1. If XAMPP Read this
  2. If Wamp Read This

Note: If you have live server, upload it to live. Its easy to check than configure local settings and all.

Your email code is ok, but if you are using wamp server .....need to change these following things...

  1. Access your email account. Click the Gear Tool > Settings > Forwarding and POP/IMAP > IMAP access. Click "Enable IMAP", then save your changes
  2. Run your WAMP Server. Enable ssl_module under Apache Module.
  3. Next, enable php_openssl and php_sockets under PHP.
  4. ** set sendmail_path both php.ini file on "C:\\wamp\\bin\\php\\php5.5.12\\php.ini" and "C:\\wamp\\bin\\apache\\apache2.4.9\\bin\\php.ini" **

sendmail_path = "C:\\wamp\\sendmail\\sendmail.exe -t"

  1. Restart Wamp Server.

I think it will be worked.

in config/email.php , i had to add

$config['smtp_crypto'] = "tls";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM