Now we are using our own server to send email to our customers. its possible to send email using google server. how to do this. explain with php codes
Download PHPMailer from http://phpmailer.sourceforge.net
Extract to folder phpmailer
Create a file email.php
Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
function email($to, $subject, $body){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->SetFrom("anything#domain.com", "Any Thing");
if(is_array($to)){
foreach($to as $t){
$mail->AddAddress($t);
}
}else{
$mail->AddAddress($to);
}
$mail->Subject = $subject;
$mail->Body = $body;
$mail->Send();
unset($mail);
}
Download http://phpmailer.sourceforge.net/ and name it "class.phpmailer.php"
http://micrub.info/2008/09/22/sending-email-with-zend_mail-using-gmail-smtp-services/
http://stackoverflow.com/questions/36079/php-mail-using-gmail
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
I am newbie and i'm trying to send mail using php but unable to send mail when i click on submit button new window open which contain some part of my php code......i want to send email from user email id to my email id with the content of form
This is the first time i'm using php and i stuck in this
<?php
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
require 'class.smtp.php';
// require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "example#gmail.com";
$mail->Password = "example";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = "example#gmail.com";
$mail->FromName = "ADMIN";
$mail->addAddress("example#gmail.com", "User 1");
$mail->IsHTML(true);
$mail->Subject = "form Submission";
$subject = $_POST['subject'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$name = $_POST['name'];
$company = $_POST['company'];
$city = $_POST['city'];
$message = $_POST['message'];
$mail->Body = "
<html>
<h2><br>Name: $name</br>
<br> Email: $email</br>
<br> Phone: $phone</br>
<br> Subject: $subject</br>
<br> Company: $company</br>
<br> City: $city</br>
<br> Message: $message </br></h2>
</html>";
$mail->AltBody = '';
if (! $mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
html--> action="http://myIPhere:7070/projectname/sendemail.php"
please help me to resolve this problem
i'm using tomcat server 9.0
my entire php code got print i think php code is not executing
in my webcontent i added
class.smtp.php
class.phpmailer.php
class.PHPMailerAutoload.php
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "test#gmail.com";
$mail->Password = "test#12#";
$mail->AddReplyTo("test#gmail.com", "test mail");
$mail->AddAddress('test#gmail.com', 'test mail ');
$mail->Body = "message:hiii";
if ($mail->send())
//if (mail($subject,$message, $headers))
{
echo "Thank you for sending mail us!";
} else {
echo "Mailed Error: " . $mail->ErrorInfo;
}
?>
this is the sample code..you can download smtp library function from github....
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers (smtp.gmail.com if you can use Gmail Account)
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#gmail.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
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Use this configuration and download PHPMailer from GitHub.
https://github.com/PHPMailer/PHPMailer
I am sending bulk of mails from fetching in database
function sendMail($subject,$to,$cc,$message){
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'myhostname'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Username = 'myusername';
$mail->Password = 'pass';
$mail->From = 'myaddress';
$mail->FromName = 'Test';
$mail->addAddress($to, ''); // Add a recipient
$mail->addCC($cc); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = ''.$subject;
//$mail->AddEmbeddedImage('mail_logo.png', 'logoimg', 'mail_logo.png');
$mail->Body = $message;
$mail->AltBody="This is text only alternative body.";
if(!$mail->send()){
//echo $mail->ErrorInfo;
return false;
}
else
{
return true;
}
}
how to know users read the email or not as well as delivery report on mails???
I'am newbie in php. I'am trying to send email using php but I don't know what's wrong in my code. I googled a lot but nothing has worked yet. Here is my php code. I'am using class.phpmailer.php.
<?php
require("phpmailer-master/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemail#googlemail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$webmaster_email = "recipient#googlemail.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="myname"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I could finally send a mail using php. Here is the code:
<?php
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'sender#mail.com';
$mail->Password = 'sender_password';
$mail->SMTPAuth = true;
$mail->From = 'sender#mail.com';
$mail->FromName = 'sender';
$mail->AddAddress("sender#mail.com");
$mail->AddReplyTo("sender#mail.com", 'Information');
$mail->IsHTML(true);
$mail->Subject = "Sample exmple to check proper working of mail function";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello ";
$path = $_POST['upload'];
$mail->AddAttachment($path); // attachment
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
?>
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
// Comment out this line here it is wrong
// IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email = "username#domain.com"; // Recipients email ID
$name = "name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Webmaster");
$mail->WordWrap = 50; // set word wrap
// i would also comment out these lines, get it working without attachments first
// then add then back in after (if you want attachments)
// $mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
// $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
// at the end of the pasted code above, you have these lines (below here) doubled up.
// remove them
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.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"; // 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 = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");
Hi I am using phpMailer For sending email. Its working fine on server, but when i am trying to send mail from my local machine(localhost) it giving me error. I am using GMAIL smtp
<?
require("lib/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "XXX#XXX.com"; // SMTP username
$mail->Password = "XXXXXXX"; // SMTP password
$mail->From = $email;
$mail->AddAddress("XXXXXX#XXX.com", "XX XX XX");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
?>
and the error is
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
It's working fine in my server.
Okay I got it in local here is the code that I used
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>