send email on localhost with PHP - php

I want to send an email on localhost but don't really know how to do it.
I tried some different ways but it doesn't work.
I used PHPMailer https://github.com/PHPMailer/PHPMailer/tree/5.2-stable as the mailserver but I think thats maybe wrong implemented or so.
Don't know if it's important but I use MAMP.
This is what I currently have:
<?php
if (isset($_POST['submit'])) {
require("PHPMailer/PHPMailerAutoload.php");
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Username = "mail account";
$mail->Password = "password for account";
$mail->Port = "465";
$mail->setFrom('receiver mail', 'TEST');
$mail->addReplyTo('receiver mail', 'TEST');
$mail->addAddress('recipient mail');
$mail->Port = "465";
$mail->isHTML(true);
$mail->Subject = "test";
// get text from input fields
$email = $_POST['email'];
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$number = $_POST['number'];
$textarea = $_POST['textarea'];
$bodyContent =
"<p>Name: " . $name . "</p>
<p>E-Mail: " . $email . "</p>
<p>Telefonnummer: " . $number . "</p>
<p>Adresse: " . $address . $city . "</p>
<p>Anliegen: " . $textarea . "</p>";
$mail->Body = $bodyContent;
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
}
?>

Comment out the following two lines :
// ini_set("SMTP","ssl://smtp.gmail.com");
// ini_set("smtp_port","465");
And add the following under the line with $mail = new PHPMailer();
$mail->isSMTP();
And it will work, I have tried it on my laptop on XAMPP.

Okay, i finally found the solution. I updated my code to this
<?php
if (isset($_POST['sendButton'])) {
require("PHPMailer/PHPMailerAutoload.php");
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "secret";
$mail->Password = "secret";
$subject = utf8_decode('test');
$mail->setFrom('secret', $subject);
$mail->addReplyTo('secret', $subject);
$mail->addAddress('secret');
$mail->Subject = utf8_decode('test');
$mail->Port = "587";
$mail->isHTML(true);
$email = $_POST['email'];
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$number = $_POST['number'];
$sendText = $_POST['sendText'];
$bodyContent =
"<p>Name: " . $name . "</p>
<p>E-Mail: " . $email . "</p>
<p>Telefonnummer: " . $number . "</p>
<p>Adresse: " . $address . ' ' . $city . "</p>
<p>Anliegen: " . $sendText . "</p>";
$mail->Body = $bodyContent;
}
?>
Besides I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON"
Now everything is fine.
Thank you for your help guys

Related

How to submit a contact form in wordpress using phpmailer and where to redirect it

where to put this configuration code
session_start();
require 'phpmailer/class.phpmailer.php';
require 'phpmailer/class.smtp.php';
$result="";
if(isset($_POST['submit'])){
$to = "username";
$from = "username";
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$message = $_POST['msg'];
$msg = 'Full Name : '. $name . " " . "\n\n<br>" . 'Corporate Email : ' . $_POST['email']."\n\n<br>"
. 'Mobile : ' . $_POST['mobile']."\n\n<br>" . 'Company : ' . $_POST['company'] . "\n\n<br>" .
'Subject
: ' . $_POST['subject'] . "\n\n<br>" . 'Message : ' . $_POST['msg'];
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.rediffmailpro.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption`ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('username', 'Seema User'); // Add a recipient
// Name is optional
$mail->addReplyTo('username', 'Seema');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_post['subject'];
$mail->Body = $msg;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send())
{
$result="Something went wrong......";
}
else{
$result="Your inquiry has been submitted, our team will get in touch with you shortly.";
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
Normally we include it in that same file or can make another file to control it but wordpress has it's own rules to control it. Can anyone explain how to use phpmailer code in wordpress step by step.
I would be greatful to you if you will help me.

PHP SMTP Custom contact form

I have this form, that send fine locally, but when i upload it to hostgator i get the following error message
Mailer Error: Could not instantiate mail function.
my php code is
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>
I have spoken to my host and they said its not within there support to deal with these things, but said my port and host are correct.
So now I'm rather confused. is there anything obvious I'm missing?
Just to let you know if anyone finds this, my problem was i was missing $mail->IsSMTP(); from my config.
the SMTP config section should be as follows
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->IsSMTP();
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>

Customize Body of SMTP Mail Script

I have a basic smtp mail script that works great but the only thing that i need is to customize body of mail. I tried to define and insert values into the script but somewhere the code gives me error. Where I'm doing wrong?
<?
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.host.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "info#host.com"; // SMTP username
$mail->Password = "123456"; // SMTP password
$mail->From = "info#host.com";
$mail->Fromname = "John Doe";
$mail->AddAddress("info#gmail.com","John Doe");
$mail->Subject = $_POST['content'];
$mail->Body = (".$arrival_date.", ".$arrival_pickup_location.", ".$arrival_dropoff_location.", ".$arrival_flight_number.", ".$arrival_fligth_time.", ".$arrival_dropoff_adress.", ".$phone_number.", ".$reservation_name.", ".$email_adress.", ".$country.", ".$additional_requests.");
$arrival_date = $_POST['arrival_date'];
$arrival_pickup_location = $_POST['arrival_pickup_location'];
$arrival_dropoff_location = $_POST['arrival_dropoff_location'];
$arrival_flight_number = $_POST['arrival_flight_number'];
$arrival_fligth_time = $_POST['arrival_fligth_time'];
$arrival_dropoff_adress = $_POST['arrival_dropoff_adress'];
$reservation_name = $_POST['reservation_name'];
$phone_number = $_POST['phone_number'];
$email_adress = $_POST['email_adress'];
$passenger = $_POST['passenger'];
$country = $_POST['country'];
$additional_requests = $_POST['additional_requests'];
if(!$mail->Send())
{
echo "Mesagge not delivered <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message sent";
?>
You're setting the variables after you're trying to set the $mail->Body object with them.
Try:
<?php
$arrival_date = $_POST['arrival_date'];
$arrival_pickup_location = $_POST['arrival_pickup_location'];
$arrival_dropoff_location = $_POST['arrival_dropoff_location'];
$arrival_flight_number = $_POST['arrival_flight_number'];
$arrival_fligth_time = $_POST['arrival_fligth_time'];
$arrival_dropoff_adress = $_POST['arrival_dropoff_adress'];
$reservation_name = $_POST['reservation_name'];
$phone_number = $_POST['phone_number'];
$email_adress = $_POST['email_adress'];
$passenger = $_POST['passenger'];
$country = $_POST['country'];
$additional_requests = $_POST['additional_requests'];
$message = '<ul><li>' . $arrival_date . "</li>";
$message .= '<li>' . $arrival_pickup_location . "</li>";
$message .= '<li>' . $arrival_dropoff_location . "</li>";
$message .= '<li>' . $arrival_flight_number . "</li>";
$message .= '<li>' . $arrival_fligth_time . "</li>";
$message .= '<li>' . $arrival_dropoff_adress . "</li>";
$message .= '<li>' . $phone_number . "</li>";
$message .= '<li>' . $reservation_name . "</li>";
$message .= '<li>' . $email_adress . "</li>";
$message .= '<li>' . $country . "</li>";
$message .= '<li>' . $additional_requests . "</li></ul>";
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.host.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "info#host.com"; // SMTP username
$mail->Password = "123456"; // SMTP password
$mail->IsHTML(true);
$mail->From = "info#host.com";
$mail->Fromname = "John Doe";
$mail->AddAddress("info#gmail.com","John Doe");
$mail->Subject = $_POST['content'];
$mail->Body = $message;
if(!$mail->Send())
{
echo "Mesagge not delivered <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message sent";
?>
Also, I don't think that using $mail->AddAddress = ("info#gmail.com","John Doe"); would work, not sure ...
.. perhaps it does though! The way above in my answer definitely should work though xD!
What's the error that you're getting? Where did it start producing the error?
$mail->AddAddress("info#gmail.com","John Doe") doesn't have an "=" sign.
so it should read:
$mail->AddAddress = ("info#gmail.com","John Doe");
also, you can add html to the body so it reads a little nicer:
$mail->Body = "<html><body>";
$mail->Body .= "<table><tbody>";
$mail->Body .= "<tr><td>".$arrival_date."</td></tr>";
$mail->Body .= "</tbody></table>";
$mail->Body .= "</body></html>";

How to send multiple emails in PHP and MySQL via phpmailer

I have a problem. When i click send button, just zxc#hotmail.com receives the message.
How to change it? I'm use Gmail SMTP send out.
There are 2 records in the database:
Here is my code:
include "phpmailer/class.phpmailer.php";
$host = "localhost";
$user = "root";
$pass = "root";
$db = "mailTest";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$query = "SELECT email FROM list";
$recordset = mysql_query($query);
$row_recordset = mysql_fetch_assoc($recordset);
$tota_row_recordset = mysql_num_rows($recordset);
$msg = strip_tags($_POST['msg']);
$mail= new PHPMailer(); //建立新物件
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail->AddAddress($to, "Test Message");
}
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->CharSet = "big5";
$mail->Subject = "Test Message";
$mail->Username = "xxxxxx";
$mail->Password = "xxxxxx";
$mail->Body = "$msg";
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: index.php');
}
With this loop you overwrite the recipient with each iteration, so only the last one stays to receive the email:
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
}
$mail = new PHPMailer();
Change your code to
$mail = new PHPMailer(); // create object FIRST
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail->AddAddress($to, "Test Message"); // add each DB entry to list of recipients
}
try this below:
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail= new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->CharSet = "big5";
$mail->Subject = "Test Message";
$mail->Username = "xxxxxx";
$mail->Password = "xxxxxx";
$mail->Body = "$msg";
$mail->IsHTML(true);
$mail->AddAddress($to, "Test Message");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
if(isset($_POST['sbmit']))
{
for($i=0;$i<count($_POST['email_address']);$i++)
{
$to = $_POST['email_address'][$i];
echo $to."<br />";
$subject = 'Thank you for Subscribing';
$message = $_POST['news'];
$headers = 'From: dhakalneil#gmail.com' . "\r\n" .
'Reply-To: xyz#hotmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo $_POST['email_address'][$i]."<br />";
ini_set("SMTP","smtp.ntc.net.np" );
ini_set('sendmail_from', 'dhakalneil#gmail.com');
if( mail($to,$subject,$message,$headers))
{
echo "Mail has been sent to admin<br />";
}
else
{
echo "Mail could not been sent";
}
}
}
?>

php phpmailer always go to hotmail junk folder anyone can solve

<?php
$to=$_POST['email'];
$pwd=$_POST['pwd'];
$activate_code = md5($_POST['pwd'] . $_POST['email'] . time());
$subject = "卓能脹戶啟動程序" ;
$body ="親愛的 [$username] : <br />" .
"歡迎加入卓能會員網站! <br />" .
"請利用以下網址啟動您的帳號,才能完成註冊程序," .
"<a href=http://www.domain.com/activate.php?code=$gencode>http://www.domain.com/activate.php?code=$gencode</a> " .
"勿直接回覆此信 。 <br />" .
"<br /> 開啟動成功後您會員帳號即可立即使用。";
include("PHPMailer/class.phpmailer.php");
$mail= new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.domain.com";
$mail->Port = 25;
$mail->CharSet = "utf-8";
$mail->Username = "minniekwok223#domain.com";
$mail->Password = "123456";
$mail->From = "minniekwok223#domain.com";
$mail->FromName = "minnie kwok";
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress($to);
?>
who always go to hotmail junk folder anyone can help ?

Categories