I am trying to configure google SMTP - php

I am trying to configure google SMTP but everytime I am getting SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed error, I've tried multiple solutions but nothing is working here. How to enable smtp mail service. here is my code
<html>
<head>
<meta charset="UTF-8">
<title>Send email via Gmail SMTP server in PHP</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="main">
<h1>Send Email via Gmail SMTP Server in PHP</h1>
<div id="login">
<h2>Send Email</h2>
<hr/>
<form action="index.php" method="post">
<input type="text" placeholder="Enter your Gmail ID" name="email"/>
<input type="password" placeholder="Enter your Gmail Password" name="password"/>
<input type="text" placeholder="To : Email Id " name="toid"/>
<input type="text" placeholder="Subject : " name="subject"/>
<textarea rows="4" cols="50" placeholder="Enter Your Message..." name="message"></textarea>
<input type="submit" value="Send" name="send"/>
</form>
</div>
</div>
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send'])){
$mail = new PHPMailer;
$mail->SMTPDebug = 2; //Enable SMTP debugging.
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'smtp.gmail.com';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $email;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
$mail->SMTPSecure = 'tls';/*Enable encryption, 'ssl' also accepted*/
$mail->From = 'abc';
//$mail->FromName = $name;
$mail->addAddress('mmmmmm#gmail.com', 'demo');/*Add a recipient*/
//$mail->addReplyTo($email, $name);
/*$mail->addCC('cc#example.com');*/
/*$mail->addBCC('bcc#example.com');*/
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(true);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}else{
header("Location: http://cetpatechnologies.com/mail/index.php");
}
}
?>
</body>

Related

Using PHP to require and modify html content to send as html-email

I am using PHPmailer to send my email. I have created a form that you enter a few details (including the email address). Once I click submit/generate I want to modify the content of the email message using the form details. The email message should show up as an html email. However, I just cannot get my form to display correctly or get my html-email to send correctly.
Form Code (index.php):
`
<?php
//PHP mailer code
require 'PHPmailer/PHPMailerAutoload.php';
$emailmessage = require 'mail.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
if (isset($_POST['submit'])) {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email#email.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('email#email.com', 'Me');
$mail->addAddress($_POST['emailrecipients'], $_POST['client']); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['callid'].' | Customer Feedback';
$mail->Body = $emailmessage;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" id="firstname" name="firstname" placeholder="John" />
<input type="text" id="lastname" name="lastname" placeholder="Doe" />
<input type="email" id="email" name="email" placeholder="email#email.com" />
<button type="submit" name="submit">Generate</button>
</form>
</body>
</html>`
Email Code (mail.php):
<!DOCTYPE html>
<html>
<body>
<p>First Name: <?php $_POST['firstname']; ?></p>
<p>Last Name: <?php $_POST['lastname']; ?></p>
</body>
</html>
I had imagined that upon submitting, the required email message would be altered as per the input fields, then sent to address typed into the the email input field.
I'm not sure if this line works as expected:
$emailmessage = require 'mail.php';
You should have your mail.php as followed:
$emailmessage = "
<!DOCTYPE html>
<html>
<body>
<p>First Name:".$_POST['firstname']."</p>
<p>Last Name: ".$_POST['lastname']."</p>
</body>
</html>";
And then use only
require "mail.php"
in line 2 of your code. You also should add the "form" Tags to your HTML-form.

PHPMailer: Sending Failed

Im trying to make a contact form, I searched alot on the web but didn't found any solution that works.
This is contact-form HTML and PHP:
<?php
if(isset($_POST['submit']))
{
$message=
'Name: '.$_POST['name'].'<br />
Subject: '.$_POST['message'].'<br />
Email: '.$_POST['email'].'
';
require 'class.phpmailer.php';
require 'PHPMailerAutoload.php';
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587; //Gmail SMTP port
// Authentication
$mail->Username = '**********#gmail.com'; // Your full Gmail address
$mail->Password = '**********'; // Your Gmail password
$mail->SMTPDebug = 1;
// Compose
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("*********#gmail.com", "Recipient Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="name" placeholder="Full Name" />
<br />
<input type="text" name="message" placeholder="Message" />
<br />
<input type="text" name="email" placeholder="Email" />
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
I get this Error.
I have the latest version of PHPMailer, Username and Password are correct.
Can someone please help?

How to resolve 'SMTP Error: Could not connect to SMTP host.' in phpmailer?

I'm trying to send email using my gmail account but i am getting an error
"SMTP Error: Could not connect to SMTP host".
I have tried port 587,465 25 but still its not working.
<?php
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->SMTPSecure = "tls";
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "rhlsngh302#gmail.com"; // Your full Gmail address
$mail->Password = "*********"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("rhlsngh302#gmail.com", "RahulName"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" />
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" />
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
How can I solve this problem?
Try:
$PHPMailer->SMTPOptions = array (
'ssl' => array (
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
works for me!
TLS on port 465 will not work - that port expects SSL. You should change it to port 587. You should not change to ssl for encryption as it's been deprecated since 1998.
It would also help if you read the docs and base your code on an up-to-date example.
First of all, while initiating PHPMailer send in the parameter true like,
$mailer = new PHPMailer(true);
This will help you to catch and handle exceptions.
Secondly, try this
$mailer->SMTPSecure = 'ssl'; // instead of tls

Bulk Email using form not working

I am trying to send bulk email from gmail using php in which from address,to address and message are set from a form. But it is not working..shows error
Mail.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
date_default_timezone_set('Etc/UTC');
require '../Mail/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "username#gmail.com";
$mail->Password = "passwrd";
$mail->SetFrom("$_POST('from')","$_POST('from_name')");
$mail->AddReplyTo("$_POST('from')","$_POST('from_name')");
$mail->AddAddress("$_POST('to')","$_POST('from_name')");
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML("$_POST('message')");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Index.php
<form action="mail.php" method="POST">
<div>
From:
<input type="text" name="from" />
Name:<input type="text" name="from_name" />
</div>
<div>
to:
<input type="text" name="to" />
Name:<input type="text" name="to_name" />
</div>
<div>
Message:
<textarea name="message"></textarea>
</div>
<input type="submit" value ="Submit"/>
</form>
it shows the follwing error:
Invalid address: Array('from')
Invalid address: Array('from')
Invalid address: Array('to')
Mailer Error: You must provide at least one recipient email address.
Someone please help me to solve this problem. I am not getting what the problem is.
Cargo-cult programming:
$mail->SetFrom("$_POST('from')","$_POST('from_name')");
should probably just be
$mail->SetFrom($_POST['from'], $_POST['from_name']);
Note the use of [] instead of (), and the LACK of " quotes around those single values.
You'll have to fix up every line of code in your script where you're doing this sort of bad array referencing.

adding attachment from form while sending mail from gmail via php

i am trying to add atatchment while sending mail from gmail using php. But it shows error...it says Could not access file: hai.jpg
follwing is the code i use
gmail.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
access to that
date_default_timezone_set('Etc/UTC');
require '../Mail/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "name#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "passwrd";
$mail->SetFrom($_POST['from'], $_POST['from_name']);
$mail->AddReplyTo($_POST['from'],$_POST['from_name']);
$mail->AddAddress($_POST['to'],$_POST['to_name']);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML($_POST['message']);
$mail->AltBody = 'This is a plain-text message body';
$mail->AddAttachment($_POST['attachment'],'application/octet-stream');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
index.html
<form action="gmail.php" method="POST">
<div>
From Name:<input type="text" name="from_name" />
From:<input type="text" name="from" />
</div>
<div>
To Name:<input type="text" name="to_name" />
To:<input type="text" name="to" />
</div>
<div>
Message:<textarea name="message"></textarea>
</div>
<div>
Attachment:<input type="file" name="attachment" />
</div>
<input type="submit" value ="Submit"/>
</form>
I dont know what if i am doing the right way here.Can anyone please guide me through this?
I've notice the absence of two itens in your code:
The use of the php $_FILES variable, which stores information about the uploaded files
The use of enctype attribute in your form element, which is required to upload files along with the form.
So, your code must treat this two itens.
Your form will look like this:
<form action="gmail.php" method="POST" enctype="multipart/form-data">
<div>
From Name:<input type="text" name="from_name" />
From:<input type="text" name="from" />
</div>
<div>
To Name:<input type="text" name="to_name" />
To:<input type="text" name="to" />
</div>
<div>
Message:<textarea name="message"></textarea>
</div>
<div>
Attachment:<input type="file" name="attachment" />
</div>
<input type="submit" value ="Submit"/>
</form>
And your code may treat the $_FILES variable:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
//access to that
date_default_timezone_set('Etc/UTC');
require '../Mail/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "name#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "passwrd";
$mail->SetFrom($_POST['from'], $_POST['from_name']);
$mail->AddReplyTo($_POST['from'],$_POST['from_name']);
$mail->AddAddress($_POST['to'],$_POST['to_name']);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML($_POST['message']);
$mail->AltBody = 'This is a plain-text message body';
//Attachs the file only if it was uploaded by http post
if (is_uploaded_file ($_FILES['attachment']['tmp_name'])) {
$mail->AddAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'], 'base64',$_FILES['attachment']['type']);
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Its not quite that straight forward uploading a file from the client browser to the server.
Here's a simple tutorial

Categories