Sending php mail, adding country code - php

I add to country code input area but country code not included when sending mail. When you fill in your phone number, it looks like the picture in the picture 1 but I want you to look like the picture 2. How do I edit this in php mailer? The codes index.php and mail.php are below. (I corrected the picture by checking the item in order to be an example) If you need JS Code
http://allinoneinvest.com/landing/intlTelInput.js
Picture 1
Picture 2
index.php phone input code
<div class="lp-pom-form-field clearfix" id="container_phone">
<label for="phone" class="main lp-form-label" id="label_phone">Phone *</label><input type="tel" id="mobile-number" oninput="this.value = this.value.replace(/[^0-9+.]/g, '').replace(/(\..*)\./g, '$1');" name="phone" class="text form_elem_phone" required>
</div>
mail.php
<?php
if (isset($_POST["submit"])) {
$isimsoyisim = $_POST['your_name'];
$email = $_POST['your_email'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$message = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
//$mail->SMTPSecure = 'tls';
$mail->Host = "mailhost";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet ="utf-8";
$mail->Username = "mail";
$mail->Password = "mailpwd";
$mail->SetFrom("$email", "$isimsoyisim");
$mail->AddReplyTo("$email", "$isimsoyisim");
$mail->AddAddress("email");
$mail->Subject = "Landing Page - $isimsoyisim";
$mail->Body = "<strong>Message:</strong><br />$message <br /><br /><strong>Mail:</strong> $email<br /><br /><strong>Phone:</strong> $phone<br /><br /><br /><br /> This message was sent from the landing page";
if(!$mail->Send()){
echo "Error: ".$mail->ErrorInfo;
} else {
$gittiMesaji = "Dear $isimsoyisim Your Message Sent";
}
}
?>

Related

How would I get the phone number and name displayed on emails I recieve - PHPMailer

So the emails I recieve only shows the subject, who it was sent from, who it was sent to but not the senders name or phone number, this is not done locally but from where my website is hosted. I want it to show the senders name and phone number after the body. This is my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $_POST['message'];
$mail->Name = $_POST['name'];
$mail->Phone = $_POST['number'];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
//echo $mail->ErrorInfo;
?>
This is my html:
<form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">
<div class="input-grids">
<input type="text" name="name" placeholder="Full name or Business"
class="contact-input" required=""/>
<input type="email" name="email" placeholder="Your email" class="contact-
input" required=""/>
<input type="text" name="subject" placeholder="Subject" class="contact-
input" required=""/>
<input type="text" name="number" placeholder="Phone number"
class="contact-input" />
</div>
<div class="form-input">
<textarea name="message" placeholder="Type your message here"
required=""></textarea>
</div>
<div class="form-input mb-5">
<label class="container"><p>Send me a copy <a href="#">privacy policy.
</a></p>
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
<button class="btn submit">Submit</button>
</form>
I am not sure if I need to do something like this:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
}
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
and just give my submit button this attribute
<button class="btn submit" name="submit">Submit</button>
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You basically want to create a $message variable and assign that to $mail->Body.
The second example you have shown is close but you need to make sure that the below is within your $_POST['submit'] if statement.
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
Like so:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
$response = $mail->send(); //to actually send the email
}
For:
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You could subject the form via ajax and listen to the $response value. If its 1 then good to go, else there was an issue.

Php script to sending email doesn't work

I created a simple form on my page and now I tried to add php script to sending email. Unfortunately it does not work. After clicking on the button, I want the user to remain on my side without redirection.
mail_sender.php
<?php
if(isset($_POST['submit'])){
$to = "someone#gmail.com";
$from = $_POST['email'];
$message = " You received the fallowing message:" . "\n\n" . $_POST['message'];
mail($to,$message,$from);
echo "Mail Sent. Thank you, we will contact you shortly.";
}
?>
HTML
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" id="submit" value="Send">
</form>
First of all name attribute is missing in your submit button. And php mail function is wrong.
It should be:
$subject = "Your subject";
$headers = "From: $from ";
mail($to,$subject,$message,$headers);
instead of:
mail($to,$message,$from);
PHP's default mail() function doesn't work most of the times, especially with GMail. This is because your e-mail needs to be formatted in a special way to be accpeted by Google's mail server. You'll be better off using a mail library like PHPMailer.
Here's how to send an e-mail using PHPMailer from a GMail Account.
$mail = new PHPMailer();
// ---------- adjust these lines ---------------------------------------
$mail->Username = "xxx#gmail.com"; // your GMail user name
$mail->Password = "passwd"; // your GMail Password
$mail->AddAddress("yyy#gmail.com"); // recipients email
$mail->FromName = "Your Name"; // readable name
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
//----------------------------------------------------------------------
if(!$mail->Send())
{
echo "mail sent";
}
I tried everything and now i received message SMTP ERROR: Failed to connect to server and SMTP connect() failed
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" name="submit" id="submit" value="Send">
</form>
PHP
<?php
require "PHPMailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer();
$to = "someone#gmail.com"; // required
$from = $_POST['email'];
$comment =
'Email: '.$from.' <br> />
Message: '.$_POST['message'].' <br> />'
;
$mail->Username = "someone#gmail.com"; // your GMail user name
$mail->Password = "Password"; // your GMail Password
$mail->AddAddress("someone#gmail.com"); // recipients email
$mail->setFrom($_POST['email']);
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Subject = 'Here is the subject';
//----------------------------------------------------------------------
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

Send html form in php with smtp

Update: My send.php now looks like this and when i click send got a blank page and send nothing. I have the class.phpmailer.php in the right place.
<html>
<head>
<title>PHPMailer - SMTP basic test with authentication</title>
</head>
<body>
<?php
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.******.hu"; // 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->Host = "mail.******.hu"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "support#******.hu"; // SMTP account username
$mail->Password = "******"; // SMTP account password
$mail->SetFrom('support#******.hu', 'First Last');
$mail->AddReplyTo("support#******.hu","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$address = "support#******.hu";
$mail->AddAddress($address, "support#******.hu");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$address = "support#******.hu";
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$formcontent= eregi_replace("[\]",'',$formcontent);
$mail->MsgHTML($formcontent);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Helo!
I want to send a html form in email with this php code but not working...
I don't know why... If you know the solution please help me :)
Thank you!
The HTML form:
<div id="stylized" class="myform">
<form id="form1" action="send.php" method="POST">
<label>Név
<span class="small">Kérem adja meg nevét</span>
</label>
<input type="text" name="name">
<label>Email
<span class="small">Kérem valós címet adjon meg</span>
</label>
<input type="text" name="email">
<br />
<br />
<label>Telefon
<span class="small">Visszahíváshoz adja meg telefonszámát</span>
</label>
<input type="text" name="phone">
<br />
<br />
<label>Elérhetőség
<span class="small">Kérem adja meg mikor érhetjük el telefonon</span>
</label>
<select name="priority" size="1">
<option value="Low">Délelőtt</option>
<option value="Normal">Délután</option>
<option value="High">Este</option>
<option value="Emergency">Egész nap</option>
</select>
<br />
<br />
<br />
<label>Szolgáltatás
<span class="small">Mivel kapcsolatban keres minket?</span>
</label>
<select name="type" size="1">
<option value="update">Szolgáltatás Megrendelése</option>
<option value="change">Szolgáltatás Lemondás</option>
<option value="addition">Információ</option>
<option value="new">Hiba Bejelentése</option>
</select>
<br />
<br />
<br />
<label>Tárgy
<span class="small">Írja le az üzenet tárgyát</span>
</label>
<input type="text" name="website">
<br />
<br />
<br />
<label>Üzenet
<span class="small">Írja le üzenetét</span>
</label>
<textarea name="message" rows="15" cols="29"></textarea><br />
<button type="submit" value="Send" style="margin-top:15px;">Küldés</button>
<div class="spacer"></div>
</form>
</div>
And my php:
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
$mail = new PHPMailer();
$formcontent = eregi_replace("[\]",'',$formcontent);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.myserver.hu"; // 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->Host = "mail.myserver.hu"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "support#myserver.hu"; // SMTP account username
$mail->Password = "myserverpass"; // SMTP account password
$mail->SetFrom('email#myserver.hu', 'First Last');
$mail->AddReplyTo("email#myserver.hu","First Last");
$mail->Subject = "test";
$mail->AltBody = "test"; // optional, comment out and test
$mail->MsgHTML($formcontent);
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$address = "email#myserver.hu";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Please help me if you can! :) Thaks!
You might want to add $mail->SMTPSecure = "ssl";
You can use mail basic function PHP... with this short code:
Class Mailer{
public static function sendMailNewUser($username,$firstname,$lastname,$password){
$to = $username;
$from_name = "INFO MAIL";
$from_mail = "noreply#mail.it";
$subject = "New User";
$uid = md5(uniqid(time()));
// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=utf8\r\n";
$message = "Welcome $firstname $lastname, ";
$message .= "<br><br>";
$message .= "This is your account:<br>
<b>username: $to<br>
password: $password</b><br>";
mail($to, $subject, $message, $header);
}
}
You just made one mistake you need to put all variables before use them, like this :
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$address = "email#myserver.hu";
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$formcontent= eregi_replace("[\]",'',$formcontent);
$mail->MsgHTML($formcontent);

Call to a member function MsgHTML() on a non-object in php

I have been created simple mail sending from localhost using php,
Her is the code:
HTML:
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
EMAIL.PHP:
<?php
require_once('class.phpmailer.php');
$mail->MsgHTML($body);
$body ='A sample email';
$mailer->IsSMTP();
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mailer->Host = 'smtp.gmail.com';
$mailer->Username = 'YYYYYYYYYY#gmail.com';
$mailer->Password = 'XXXXXXXXX';
?>
when run this code,
following error displayed,
Fatal error: Call to a member function MsgHTML() on a non-object online 6
note: where is got phpmailer.php source code from this link
I m new to php, but i want to know, particular this section.
Can anyone help me to fix this,
Thanks in advance,
Perhaps $mail is not instantiated, and spelled wrong (did you mean $mailer?). Also, you should set $body before MsgHTML($body). From your link, you may want to add this
$mailer = new PHPMailer;
and make changes like so:
$mailer = new PHPMailer;
$body ='A sample email';
//$mail->MsgHTML($body);
$mailer->MsgHTML($body);
$mailer->IsSMTP();
...
Here is an example of an HTML-form and PHPMailer:
HTML
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
mail.php
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require_once('class.phpmailer.php');
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer#bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_PHPMailer#bradm.inmotiontesting.com"; // SMTP username
$mail->Password = "password"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>

phpmailer secure approach by placing credentials out of the web root in INI file

first of all thank you for your time and helping me on this.
I have a simple contact form and i'm using phpmailer.
I want to store credentials in an INI file out of webroot and then include it in my mail.php file which is the mail sending script.
How to write the INI content and how to call them on mail.php file?
This is my HTML file which is contact us form:
<h3><font style="line-height:170%" size="4"> <a id="contactus">Contact us form</a> </font></h3>
<form method="post" action="email.php" accept-charset="UTF-8">
<p>
<label>your name</label>
<input name="dname" type="text" size="30" />
<label>your email</label>
<input name="demail" type="text" size="30" />
<label>your domain name</label>
<input name="ddomain" type="text" size="30" />
<label>Message</label>
<textarea name="dmessage" id="dmessage" rows="5" cols="5"></textarea>
<br />
<input class="button" type="submit" value="submit"/>
</p>
</form>
<br />
and this is the email.php file:
<?php
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = "info#example.com"; // SMTP username
$mail->Password = "this is the password"; // SMTP password
$mail->From = $email;
$mail->AddAddress("info#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
I want to store credentials in a safe place.
Another problem is that whenever someone open email.php file in browser ( example.com/email.php) an empty email will be sent to me, how to prevent it?
I want the email.php file to be executed only a result of a customer's contact via filling the form and not by directly opening the email.php file
Thank you all
<?php
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
$constants = parse_ini_file("/outside/web/sample.ini");
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = $constants['username']; // SMTP username
$mail->Password = $constants['password']; // SMTP password
$mail->From = $email;
$mail->AddAddress("info#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
INI file (sample.ini) looks like:
username = "info#example.com"
password = "PASSW0RD"
Thank you for your helps.
I did some change on your code and it is excatly what is want.
instead of saying:
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
i used this one:
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
echo "Please fill-in Email and Message sections completely!";
exit;
}
Thank you for your help

Categories