简体   繁体   中英

Not getting email using PHP

I want to send a conformation email using php code. I have tried all solutions such as mail() and phpmailer but nothing is working. Here is my code for mail()

ini_set(sendmail_from, "xyz@domainname.net" );
ini_set(SMTP, "aspmx2.googlemail.com" );
ini_set(smtp_port, 25 );
$username = "xyz";
 $password = "password";
$to=$email;
$subject="Activate you account"; 
$message="
Hello $fullname, \n \n
    ";
$headers="From: xyz@ domainname.net";
 mail($to, $subject, $message, $headers);
die ("You have been registerd. Check you email to activate your account!");

I am getting “You have been registered. Check you email to activate your account” “You have been registered. Check you email to activate your account” message on the page, but I am not getting an email.

I tried without ini_set() but it is still not working. I also have set SMTP and port in php.ini file but it is still not working.

Please help!! Thanks

You're not actually setting the username/password for the mail() call. All you're doing is setting some variables, and then not using those variables in any way for the mail. So your attempt to send mail through Google fails, because you're not authenticating.

But as other answers have said, don't use the built-in mail() function. Use PHPMailer or Swiftmailer instead. They're far easier to use, handle all the ugly stuff of doing file attachments and multi-part MIME mail, and do it all behind a nice OOP interface.

To points here:

You're calling mail without checking the return code -- which means that you know that you're attempting to send a message, but you have no idea whether it succeeds. Which isn't helpful for debugging.

And second, Gmail requires an encrypted connection before they will allow you to authenticate. And a standard SMTP connection isn't encrypted.

Try sending e-mails using PHPmailer i have heard lots of great things about it.

sample of PHPMailer config

$mail->Mailer = "smtp";  
$mail->Host = "ssl://smtp.gmail.com";  
$mail->Port = 465;  
$mail->SMTPAuth = true; // turn on SMTP authentication  
$mail->Username = "username@gmail.com"; // SMTP username  
$mail->Password = "password"; // SMTP password 

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