简体   繁体   中英

Sending Email to gmail account using PHP

I am trying to send mail to gmail account from my web site. It seems that everything is fine at the code level. Can you guys please look into this issue: even if i run this script, mail is not received to gmail Account.

 <?php
        if(isset($_POST['submit'])){
        ini_set('SMTP','localhost'); 
        $msg='Name : '.$_POST['name']."\n"
                .'Email : '.$_POST['email']."\n"
                .'Message : '.$_POST['message'];

                mail("aa@gmail.com","Message from Contact Us",$msg);
                     }

        else{
               echo 'cannot send email';
            }

        ?>

I think you will install phpmailer( http://pear.php.net/ ) and then send through smtp here is example code:

require_once "Mail.php";

$from = '<from@gmail.com>';
$to = '<to@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'your gmail ',
        'password' => 'your password'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');

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