PHP Emails with PHP Mailer - php

I'm creating a form that sends an email to the email that was entered into it. I have installed PHP mailer on my server.
Currently when sending emails via PHP they send like this:
All emails sent are via the hosting server when I'd like them to look like this.
Just like any other email sent on the domain they should be mailed by domain.com rather than the server.
I'm just testing this so using a simple form as a proof of concept.
<form method="post" name="process.php" action="process.php">
<p>Name:</p><br><input type="text" name="name"><br><br>
<p>Email Address:</p><br><input type="text" name="email"><br><br>
<br>
<input type="submit" value="Send Email">
</form>
I'm then using this PHP to send the email:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply#domain.com';
$mail->Password = 'password';
$email_from = "noreply#domain.com";
$email_subject = "Test Email";
$to = $email;
$headers = "From: noreply#domain.com \r\n";
$headers .= "Bcc: noreply#domain.com \r\n";
$headers .= "Reply-To: me#domain.com \r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$email_body = <<<EOM
<p color="#000000">Hello, $name.<br><br> This is a test email for mailing from the domain rather than the server.<br><br> </p>
EOM
;
mail($to, $email_subject, $email_body, $headers);
?>
Basically, I want PHP emails mailed by my domain and I don't know how to do this so any help would be appreciated, my web host doesn't seem able to help me with this.
Thanks in advance.
UPDATE
This code for the form.
<h1>The email gets sent to a bookings address.</h1>
<form method="post" name="process.php" action="process.php">
<p class= "whitetextsubsmall">Name:</p><br><input type="text" name="name"><br><br>
<p class= "whitetextsubsmall">Email Address:</p><br><input type="text" name="email"><br><br>
<br>
<input type="submit" value="Send Email">
</form>
And this code for process.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply#domain.com'; /* This is the sender of the bookings. */
$mail->Password = 'password';
$mail->setFrom('noreply#domain.com');
$mail->addAddress('bookings#domain.com', 'Company Bookings');
$mail->addReplyTo($email, $name); /* Reply to the user who submitted the form from the bookings email. */
$mail->Subject = 'Booking Request Test';
$mail->isHTML(TRUE);
$mail->Body = 'Message test <br> Booking Request from: $name <br><br> Email: $email.';
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

You are mixing the PHPMailer syntax with PHP mail() syntax.
For the PHPMailer, use the following in your code.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply#domain.com';
$mail->Password = 'password';
/* Set the mail sender. */
$mail->setFrom($email, $name);
/* Add a recipient. */
$mail->addAddress('noreply#domain.com', 'earningtoanimate');
/* Add a replyto. */
$mail->addReplyTo($email, $name);
/* Add a CC and Bcc. */
$mail->addCC('noreply2#domain.com', 'earningtoanimate2');
$mail->addBCC('noreply3#domain.com', 'earningtoanimate3');
/* Add email subject. */
$mail->Subject = 'Test Email';
/* Add email body. */
$mail->isHTML(TRUE);
$mail->Body = 'There goes your message.';
/* Finally send the mail. */
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
Test the above and give your feedback. Note, I didn't test code. Just wrote them here so it's possible to have some edits if need be.
For further reading visit PHPMailer documentation

Related

Which PHPMailer prop controls the sender email in the email header?

I'm getting an error from jQuery with a 404 to my mail.php (I checked the path and it seems correct)
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require("../includes/libraries/phpmailer/PHPMailer-master/src/Exception.php");
require("../includes/libraries/phpmailer/PHPMailer-master/src/PHPMailer.php");
require("../includes/libraries/phpmailer/PHPMailer-master/src/SMTP.php");
$server_email = 'myemail#outlook.com';
$pass = '123456';
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp-mail.outlook.com';
$mail->Port = 587;
$mail->Username = $server_email;
$mail->Password = $pass;
$mail->FromName = $full_name;
$mail->From = $email;
$mail->setFrom($server_email, $full_name );
$mail->addAddress($server_email);
$mail->addReplyTo($email); //email that it will be replied to
$mail->Subject = 'Subscription submitted by ' . $full_name . '.';
$mail->Body = "
Name: $full_name<br><br>
Email: $email<br>
";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
Right now, my email (the outlook one) comes up in the header of the email next to the Sender's Name. The addReplyTo actually does give me the gmail address to reply to, so that's great. But having my email show on the sender header is confusing. See the image below
Thanks in advance

How to change email sender email in PHP Mailer

Currentlt it shows the mail on which phpmailer SMTP is registered.
$email='abc#email.com';
$subjectRsc="Any";
$message='Welcome';
phpmail($email, $subjectRsc, $message);
My phpmailer function:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->AddReplyTo(REPLY_EMAIL, 'ABC');
$mail->SetFrom(FROM_EMAIL, 'ABC');
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
This is how I am sending mails to the users I want to show the specific mail of my website to be displayed in the mails not that on which smtp server is registered.
You should try this :
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->addReplyTo('myemail#example.com', 'ABC'); //<-- this is the line i changed
$mail->From= "myemail#example.com"; //<-- this is the line i changed
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
According to phpmailer documentation this is the field to add the sender's email.
please try using this code :
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("test#gmail.com", "Test");
$mail->addAddress("test1#gmail.com");
//Address to which recipient will reply
//if you want to add CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
Please try using this code:
<?php
$to = "test#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:test1#gmail.com \r\n";
$header .= "Cc:test2#gmail \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>

Click and send mail to user using A-Frame and php

I want to send email by clicking on button using aframe. I did something but that does not work. Here is working flow.
I have database and I am getting user email form database.
Here is html:
<a-plane id="information-button" name="button-press" class="clickable"
position="6 -4.845 3" rotation="0 -30 0" width="3" height="1" color="#000">
<a-text value="More Info" position="-0.946 0.077 0.349" scale="2 2 2">
</a-text>
</a-plane>
Here is email.php to send the email.
<?php
include('server.php');
session_start();
?>
<?php
if (isset($_POST['button-press'])) {
require "PHPMailer/src/PHPMailer.php";
require "PHPMailer/src/OAuth.php";
require "PHPMailer/src/SMTP.php";
require "PHPMailer/src/POP3.php";
require "PHPMailer/src/Exception.php";
require 'PHPMailer/src/PHPMailerAutoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if (isset($_SESSION['email'])){
$headers = "MIME-Version: 1.0" . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$message = '<p><strong>You are in.</strong></p>';
$subj = 'More Information';
$to = 'falak#oddly.co';
$from = 'falaksabbir3#gmail.com';
define('GUSER', 'falaksabbir3#gmail.com'); // GMail username
define('GPWD', '******');
define('SMTPUSER', 'falaksabbir3#gmail.com'); // sec. smtp username
define('SMTPPWD', '******'); // sec. password
define('SMTPSERVER', 'smtp.gmail.com'); // sec. smtp server
//send mail
if ( smtpmailer($to, $from, $name, $subj, $message,$headers)){
echo 'Message send via Gmail';
} else {
if (!smtpmailer($to, $from, $name, $subj, $message,$headers)){
if (!empty($error)) echo $error;
} else {
echo 'Yep, the message is send (after doing some hard work)';
}
}
//send email function
function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if ($is_gmail) {
$mail->SMTPSecure = 'ssl';
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
}
else {
$mail->Host = SMTPSERVER;
$mail->Username = SMTPUSER;
$mail->Password = SMTPPWD;
}
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
}}
?>
So this is how I did. Can I do php with aframe? Or any guidelines with this.
Yes you can do PHP with A-Frame.
Currently though your A-Frame code does not interact with your PHP code. You want your action (in VR or not) to call the server. I suggest to :
make the send action work by just visiting the page
make sure it works without VR, for example using a simple HTML button How to Call a PHP Function on the Click of a Button
finally make it work with VR and A-Frame probably using the cursor and a bit of JavaScript https://aframe.io/docs/0.8.0/components/cursor.html#example

inserting HTML POST form field data into PHPmailer email body

I have tried every hint I have found across the web and here but what I cannot achieve is for the sent email to contain the form data in the html formatted message. The mailing function is working and I get the email properly.
The form is basically:
<form method="post" action="test.php" name="Work Record" autocomplete="off">
<input type="text" name="first_name" id="staff_name2" placeholder="First" required />
<input type="text" name="last_name" id="staff_name" placeholder="Last" required />
<input name="checkbox" type="checkbox" class="head-l" id="checkbox" onChange="this.form.submit()">
And using the latest phpmailer from google the action script is as follows. I have the emailing function working through gmail, I am just not sure what to put where to pull the data from the form and put it into the email body. The code that is pasted in the body now is that we used to use before switching to phpmailer. Thank you!
<?php
require '/home/newnplhftp/nplh.us/smtp/phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'NPLH';
$mail->AddAddress('xxxxxxxxxxxxxxxxxxxxx'); // Name is optional
$mail->AddCC('');
$mail->AddBCC('');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'xxxxxxxxxxxxxxx';
$mail->Body = '
<html>
<h2><b>$first_name $last_name, $license_type</b></h2>
</html>';
$mail->AltBody = '';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Thank you, your message has been sent!';
?>
Please set;
$mail->Body = '
<html>
<h2><b>$first_name $last_name, $license_type</b></h2>
</html>';
to
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$license_type = $_POST['license_type'];
$mail->Body = "
<html>
<h2><b>".$first_name." ".$last_name.", ".$license_type."</b></h2>
</html>";
The variables you are trying to send need to be captured from the the form first. This would happen on test.php since this is where you are posting the values to.
So you would do this
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$license_type = $_POST['license_type'];
Then you can use them inside the mail body.
$mail->Body = "<html><h2><b>$first_name $last_name, $license_type</b></h2></html>";
Though I would highly suggest validating that data in some way first.

attachment is not sent with phpmailer

I'm trying to send an attachment with the phpMailer script.
Everything is working correctly since my email is sent correctly, however I do have a problem with an attachment that is not sent.
HTML part:
<p>
<label>Attachment :</label>
<input name="doc" type="file">
</p>
PHP:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = '*****';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = '*****';
$mail->Password = '*****';
$mail->SMTPSecure = 'ssl';
$mail->From = $_POST["name"];
$mail->FromName = 'Your Name';
$mail->Subject = 'Message Subject';
$mail->addAddress('*****');
$mail->addAttachment($_FILES["doc"]);
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = "You got a new message from your website :
Name: $_POST[name]
Company: $_POST[company]
Phone: $_POST[phone]
Email: $_POST[email]
Message: $_POST[message]";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Thanks!</title>
</head>
<body>
<p> <img src="img/correct.png" alt="icon" style=" margin-right: 10px;">Thank you! We will get back to you soon.</p>
</body>
</html>
Try:
if (isset($_FILES['doc']) &&
$_FILES['doc']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['doc']['tmp_name'],
$_FILES['doc']['name']);
}
Basic example can also be found [here](https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/AdvancedMail).
The function definition for `AddAttachment` is:
public function AddAttachment($path,
$name = '',
$encoding = 'base64',
$type = 'application/octet-stream')
Please have a look about how PHP file uploads work. This array key:
$_FILES["doc"]
... will never contain a file. At most, it'll contain an array with information about where to find the file.
Untested solution, but should work:
change
$mail->addAttachment($_FILES["doc"]);
to
$mail->addAttachment($_FILES["doc"]["tmp_name"], $_FILES["doc"]["name"], "base64", $_FILES["doc"]["tmp_type"]);
However, please consider learning php upload file handling as already commented by others. Don't use user input without validation. Never!

Categories