Send email from localhost running XAMMP in PHP usi

2019-01-01 00:52发布

I try to send an email from localhost to my yahoo email account using php mail() function, the return says I successfully send the email but I did not get any email. I've been reading and trying many so called 'simple way' to send email but the result are disappointing, none of them work for me. Below are the code, the configurations and the error message. Can someone enlighten me with this? Thanks.

php code

<?php
$to      = 'myemail@yahoo.com';
$subject = 'Fake sendmail test';
$message = 'If we can read this, it means that our fake Sendmail setup works!';
$headers = 'From: myemail@egmail.com' . "\r\n" .
           'Reply-To: myemail@gmail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
} else {
    die('Failure: Email was not sent!');
}
?>

Configuration for php.ini (I'm using gmail mail server)

SMTP =smtp.gmail.com
smtp_port =587
sendmail_from = myemail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Configuration for sendmail.ini

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
auth_username=myemail@gmail.com
auth_password=mypassword
force_sender=myemail@gmail.com

error message in sendmail error log with port 587

13/10/02 13:36:41 : Must issue a STARTTLS command first. k4sm129639pbd.11 - gsmtp

9条回答
骚的不知所云
2楼-- · 2019-01-01 01:27

TRY THIS. IT WORKS FOR ME ALWAYS.

    $config['protocol']    = 'smtp';
    $config['smtp_host']    = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';                            //ssl
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = 'tandelromal09@gmail.com';
    $config['smtp_pass']    = 'pentium409';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'html'; 
    $config['validation'] = TRUE;
查看更多
栀子花@的思念
3楼-- · 2019-01-01 01:30

Don't forget to generate a second password for your Gmail account. You will use this new password in your code. Read this:

https://support.google.com/accounts/answer/185833

Under the section "How to generate an App password" click on "App passwords", then under "Select app" choose "Mail", select your device and click "Generate". Your second password will be printed on the screen.

查看更多
不流泪的眼
4楼-- · 2019-01-01 01:31

firstly configure php.ini file which is located in your xampp/php to

[mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury SMTP = smtp.gmail.com
smtp_port = 25

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

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t -i"

remove semicolon before sendmail from and sendmailpath

after this configure sendmail.ini file which is located in sendmail folder you can search in xampp folder and do this:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
default_domain=localhost
error_logfile=error.log
auth_username=yourgmailusername@gmail.com
auth_password=yourgmailpassword
pop3_server=
pop3_username=
pop3_password=
force_sender=yourgmailusername@gmail.com
force_recipient=
hostname=localhost

then use mail function
like

mail("yourgmailusername@gmail.com","Success","Send mail from localhost using PHP");
in php save file as email.php the last thing is to make sure that two-step verification is turned off in your gmail account otherwise you will not get mail and under gmail settings > **Forwarding and POP/IMAP enable imap and save changes and lastly turn off less secure connections app
Go,have Fun

查看更多
何处买醉
5楼-- · 2019-01-01 01:33

Check your Spam . mail() function sometimes sends the mail to spam.

查看更多
人间绝色
6楼-- · 2019-01-01 01:42

in php.ini file,uncomment this one

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

and in sendmail.ini

smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=your@gmail.com
auth_password=yourpassword
force_sender=your@gmail.com
hostname=localhost

configure this one..it will works...it working fine for me.

thanks.

查看更多
看风景的人
7楼-- · 2019-01-01 01:45

Here's the link that gives me the answer:

[Install] the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip

[Modify] the php.ini file to use it (commented out the other lines):

[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25

; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

(ignore the "Unix only" bit, since we actually are using sendmail)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com

To access a Gmail account protected by 2-factor verification, you will need to create an application-specific password. (source)

查看更多
登录 后发表回答