Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, To send an email from PHP form automatically I made these changes in php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=465
auth_username = ****@gmail.com
auth_password = ****

sendmail_from = ****@gmail.com

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


In sendmail.ini

SMTP=smtp.gmail.com

smtp_port=465

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

force_sender=****@gmail.com

everything is going well I am getting Message sent successfully...message but to my gmail I am not getting any mail.


Please give your suggestions

What I have tried:

<?php
$to = "*******@gmail.com";
$subject = "This is subject";

$message = "This is HTML message.";
$message .= "

This is headline.

";

$header = "From:****@gmail.com \r\n";

$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);

if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
Posted
Updated 11-Jul-17 8:19am

1 solution

Hi,

Use PHPMailer, this library is very good. I use it in all my projects.

Library in the GitHub - PHPMailer/PHPMailer: The classic email sending library for PHP

My function to send an e-mail using PHPMailer.

PHP
public function Send_Email_Gmail($address_to, $name_to, $message)
    {
        try
        {
            //error_reporting(E_ALL);
            error_reporting(E_STRICT);
            
            //definir time zone
            date_default_timezone_set('America/Sao_Paulo');

            //inicializar class e-mail
            $mail = new phpmailer();
            $body = $message;
            
            $mail->IsSMTP(); // telling the class to use SMTP
            $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                                       // 1 = errors and messages
                                                       // 2 = messages only
            $mail->SMTPAuth   = true;                  // enable SMTP authentication
            $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
            $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
            $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
            
            //porta 465 - ssl / 587 - tls 
            
            
            $mail->Username   = my_user_name_gmail;  // GMAIL username
            $mail->Password   = my_password_gmail;            // GMAIL password
            $mail->SetFrom('my_user_name_gmail@gmail.com', utf8_decode('My e-mail'));

            $mail->Subject    = utf8_decode('send test e-mail');
            $mail->AltBody    = utf8_decode("To view the message, please use an HTML-compatible email viewer!");    
            
            $mail->MsgHTML($body);

            $mail->AddAddress($address_to, $name_to);

            if($mail->Send()) {
                return $mail->ErrorInfo;
            } else {
               
                return 'ok';
            }
        }
        catch(Exception $e)
        {
            return 'Error to send e-mail. Error: ' . $e->getMessage();
        }
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900