How do I send emails through PHP to GMail? - php

I'm guessing that this:
<?php
$emailTo = 'user1#gmail.com';
$subject = 'I hope this works!';
$body = 'Blah';
$headers='From: user#gmail.com'
mail($emailTo, $subject, $body, $headers);
?>
is not going to cut it. I searched for ways that I can send to email with SMTP auth and mail clients and programs such as PHPMailer, but I don't have a concise and direct answer yet thats helpful. Basically, I want a way to send emails to gmail, hotmail, etc (which will be my email) from another email (sender) through a form on my website
Questions:
Do I need to download a 3rd party library to this?
If not, how can I change the code above to make it work.
Thanks!

Use PHPMailer library.It is better than use mail native function because in PHPMailer you can use user authentication to avoid to send the email to spam.You can use this library without to configure a mail server. You can download in this link https://github.com/PHPMailer/PHPMailer
See an example:
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$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";
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 587; // SMTP port
$mail->Username = "yourusername#gmail.com"; // username
$mail->Password = "yourpassword"; // password
$mail->SetFrom('user#gmail.com', 'Test');
$mail->Subject = "I hope this works!";
$mail->MsgHTML('Blah');
$address = "test#test.com";
$mail->AddAddress($address, "Test");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Related

Could not instantiate mail function. Message was not sent PHPMailer Error: Could not instantiate mail function

I am using PHPMailer to send mails. I have deployed my website on Google Cloud VM Instance
Here is my mail sending code
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new \PHPMailer;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
$mail->addAddress($_POST['email']);
$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hello, Below are the details of contact ";
$mail->Body = "<table><tr><td>Name:</td><td>".$_POST['name']."</td></tr><tr><td>Email Address:</td><td>".$_POST['email']."</td></tr><tr><td>Subject :</td><td>".$_POST['subject']."</td></tr><tr><td> Message :</td>".$_POST['$POST['msg']."</td></tr></table>";
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
But the mail is not be able to send. Can anyone help me out? I can provide further code references if anyone needs
Try using SMTP to send email:
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
You have commented out isSMTP() so you are not using SMTP (so none of your SMTP settings will do anything), but the default mail() transport. This requires that you have a mail server installed in your server, which you apparently don’t.
To fix, either install a mail server (postfix is good) or enable SMTP, as chirag suggested.
All of this is covered in the PHPMailer troubleshooting guide.

PHPMailer: Don't understand the from address

I'm trying to learn PHPMailer and I don't understand why it needs an email address and password.
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
It's not coming from a persons email, it's coming from the site. What email am I supposed to put here and why?
The email address is the address which the emails are sent from. The username and password are required for SMTP servers that require authentication in order to send mail.
The way email works is that party 1 has an email account which it uses to send mail to party 2. Party 1 can be a site, a person, a script or something else, but it still needs to have an email account to be able to send email.
This is the way SMTP (Simple Mail Transfer Protocol) has been defined, and you need to follow the definitions of that protocol to get your mail delivered.
It is not necessary to provide password -
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "example#gmail.com";
$mail->AddAddress("example#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
Click here

PHPMailer for SMTP sends mail to inbox but from invalid mail address

Need help!!!
I am using php mailer in my current project. It works successfully, after sending a mail successfully to the other end it says the mail comes from the smtp server account not from the $from what i use here-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
My smtp server code here..
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "muradautorun";
$mail->Password = ""; //my passsword here
Here is my complete code-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
$from_name="S.M. Murad Hasan Tanvir";
$subject="A sending mail code";
$body ="This is a sending email code, the file attach here is about the coding.";
//here i use gmail as a smtp server
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "muradautorun";
$mail->Password = ""; //my passsword here
$mail->SetFrom($from, $from_name);
$mail->addAttachment('emailSend.php'); // Add attachment(just the file url)
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo 'Mail error: '.$mail->ErrorInfo;
}
else {
echo 'Message sent!';
}
?>
Where i doing wrong i can't find out please help me...
Thanks in advance.
You need to allow sending mail as "tanvir_cse0906#yahoo.com" on your "muradautorun" gmail account first.
You can do this by following the following steps:
Login with "muradautorun"
Click the gear in the top right and choose "Settings"
Click on the "Accounts and Import" tab
Click on link "Add another email address you own" in the "Send mail as" section, and follow the instructions.

PHP MAILER - EMAIL SENDING

I want to send a confirmation mail to an user who is trying to register. Iam using PHP Mailer for sending mails. Here is my code:
require_once("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;// turn on SMTP authentication
$mail->Username = "root";// SMTP username
$mail->Password = "";// SMTP password
$mail->SetLanguage("en","PHPMailer/language");
//compose mail
$mail->From = "admin#localhost.com";
$mail->FromName = "Cinema Admin";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo("admin#localhost.com", "Cinema Admin");
$mail->Subject = 'Your confirmation link here';
$mail->Body = "Your confirmation link\r\n";
$mail->Body .= "Click on this link to activate your sccount\r\n";
$mail->Body .= "http://localhost/user.login_plugin/confirm.php?passkey=$confirm_code";
//send mail
if (!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
When I execute the file,it says
Message was not sent
Mailer Error: SMTP connect() failed.
Could anyone suggest me for sending mails from localhost to the registered user's emailid.
I had searched with few titles regarding "ending emails" in stack overflow, but couldn't find any solution from any posts.
Thank You in advance
Could you try remove the smtp part? From your comment , you do not have mail server setup.
$mail = new PHPMailer();
$mail->SetLanguage("en","PHPMailer/language"); // remove smtp things
You must need a SMTP details in order to send an email using SMTP, For example if you want to setup SMTP using gmail,
$mail->IsSMTP(); // telling the class to use SMTP
$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
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
If you don't want use SMTP, you can called default mail function
$mail->IsMail(); //Sets Mailer to send message using PHP mail() function.

Could not send mail to other accounts apart from my own?

I've downloaded PHPMailer library from github and used the following script to send the mail. I've tried to send email to my own account and it worked. It printed "Message has been sent" and received the email in gmail. But when I tried sending mail to my friend's account, he didn't receive the email. But the script says message has been sent.
<?php
include('PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "example#gmail.com";
$mail->Password = "examplepassword";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("example2#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I'm using xampp. What's the reason behind the problem, do I need to change any settings in my gmail settings?
EDIT: Now I got mail to second address but after 40 mins. But when I send it first address, it is received immediately. Don't know why? I want to use it for email address verification, 40 mins is very long.
below link will help you:
Sending emails through PHP mail is slow
http://stackoverflow.com/questions/6380477/sending-emails-through-php-mail-is-slow

Categories