0

So I'm building my own website for fun from scratch and I wanted to create an emailing functiong. Now I wanted to make it so that the user won't have to use outlook or anything else to send me an email, I wanted it so that they could send it to me through the click of a button.

Now this is basically the gist of what I use right now

mail($to, $subject, $body);

however, this only works if I have my email set to my hosting servers email (byethost). I tried setting it to my gmail account but that didn't work.

So, what could I do to have it send an email to my gmail

Hello Mellow
  • 169
  • 2
  • 15

4 Answers4

0

You need SMTP to send mail using your gmail. PHPMailer is the one I always use to do this.

https://github.com/PHPMailer/PHPMailer

Err
  • 890
  • 2
  • 11
  • 32
0

You said you get mail in your hosting servers email (byethost) but not in the gmail account. But you should be get the mail in gmail. Will you check in your spam folder ?. if you sent mail with proper email header it should be get in your inbox. check here complete-mail-header

For avoiding server configuration issue you can use PHP mailer. You also configure IMAP and POP3 with this

Community
  • 1
  • 1
tarikul05
  • 1,843
  • 1
  • 16
  • 24
0

I had this similar case in one of the project in which the user's (site visitor) query was to be sent to site admin (gmail account). Have done this with Ajax call sending the details collected on the form, posted to php file that uses Postfix to send mail. (The website was hosted on Ubuntu Server).

The JQuery code in the page is given below.

$("#btn_email").click(function () {
            //get input field values data to be sent to server
            post_data = {

                'vemail': $('input[name=email]').val(),
                'query'  : $('input[name=query]').val(),
                'subject': 'New Query',
                'msg': 'There is new request for query'
            };

            //Ajax post data to server
            $.post('sendmail.php', post_data, function (response) {
                if (response.type == 'error') { //load json data from server and output message
                    output = '<div class="error">' + response.text + '</div>';
                } else {
                    output = '<div class="success">' + response.text + '</div>'; }}});

The PHP code that handles the request as

$email=$_POST['email'];
$subject = 'New Query';
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$message = $_POST['msg']. "\r\n" .$_POST['query'] . "\r\n\r\nEmail : " . $email ;
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
$send_mail = mail("mycontact@gmail.com", $subject, $message, $headers); 
if(!$send_mail)
{
    //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hi, We have added '.$user_email .' to the query. Will answer you soon. Thank you for your interest.'));
    die($output);
}

}
Rajesh
  • 934
  • 12
  • 23
0

If it is only for fun, one option you can consider is using Sengrid, it is very simple and effective. In byethost email() function doesn't work. Once you get your APIKEY you can set your email easily. This is the code for php from github:

$sendgrid = new SendGrid('YOUR_SENDGRID_APIKEY');
$email = new SendGrid\Email();
$email
    ->addTo('foo@bar.com')
    ->setFrom('me@bar.com')
    ->setSubject('Subject goes here')

    ->setText('Hello World!')
    ->setHtml('<strong>Hello World!</strong>')
;

$sendgrid->send($email);

// Or catch the error

try {
    $sendgrid->send($email);
} catch(\SendGrid\Exception $e) {
    echo $e->getCode();
    foreach($e->getErrors() as $er) {
        echo $er;
    }
}

And you have a free plan with plenty enough free emails per month.

Jose Gallo
  • 134
  • 1
  • 9