This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
May I know how to send an email from localhost to gmail or other email accounts? I already make research regarding this matter and tried to do it but still can not send the email. Below are my sendmail.ini and php.ini files that I edit,
sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
error_logfile=error.log
debug_logfile=debug.log
auth_username=khairulamran.nazri#gmail.com
auth_password=[email password]
pop3_server=
pop3_username=
pop3_password=
force_sender=khairulamran.nazri#gmail.com
force_recipient=
hostname=smtp.gmail.com
php.ini - mail function
SMTP = smtp.gmail.com
smtp_port = 465
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off
contact.php - to send the email
<?php
if(isset($_POST['submitted'])){
$name=$_POST['name'];
$to=$_POST['email'];
$hp=$_POST['hp'];
$subject="Test";
$msg="Thank you for Register. Your Name is ".$name." and Hp no. is ".$hp;
$header="khairulamran.nazri#gmail.com";
$success=mail($to,$subject,$msg,$header);
if($success==true){
echo "Email send successfully ";
} else{
echo "Error sending email";
}
}
?>
<form name="contact" method="post" action="">
Nama:<input type="text" name="name"><p>
Email:<input type="text" name="email"><p>
Hp:<input type="text" name="hp">
<input type="submit" name="submitted" value="Submit">
</form>
If click Submit, it will not send the email that have been submitted.
Try to use mailer library. I had used and it will work perfectly.
Follow this. https://github.com/PHPMailer/PHPMailer
You would need to only enter your gmail credentials and after that it will work.
This is the solution for my case. Hope it will be useful if someone need information regarding this case as well.
<?php
if(isset($_POST['submitted'])){
$name=$_POST['name'];
$to=$_POST['email'];
$hp=$_POST['hp'];
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('khairulamran.nazri#gmail.com', 'Amran');
$mail->addAddress($to); // Name is optional
$mail->addReplyTo('khairulamran.nazri#gmail.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test Email';
$mail->Body = 'Body of message goes here';
$mail->AltBody = 'Body of message goes here<for non html mail client>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
<form name="contact" method="post" action="">
Nama:<input type="text" name="name"><p>
Email:<input type="text" name="email"><p>
Hp:<input type="text" name="hp">
<input type="submit" name="submitted" value="Submit">
</form>
Related
I am very new to php and im trying to set up a postmaster, I am using Hostbuddy for my hosting site and downloaded their smtp information for my server.
I got these files from them automatically uploaded to my server for testing the smtp:
class.phpmailer.php
class.smtp.php
index.php
web.config
I think i am entering the correct information for the test. but i keep getting a callback that says, "Could not connect to smtp server".
Here is the index.php so you guys can see what is going on.
<Center><h2>PHP Test Email Script</h2></center>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" target="_blank">
<Table align = center>
<tr><td>From Email: <td><input type="text" name="uname"> i.e yourname#yourdomain.com</tr>
<tr><td>Email Password: <td><input type="password" name="pass"></tr>
<tr><td>Host: <td><input type="text" name="host"> i.e Mail.YourDomain.com</tr>
<tr><td>To:<td> <input type="text" name="to"></tr>
<tr><td colspan =2 align = center><input type="submit" name="submit" value="Send Email"></tr>
</table>
</form>
<?php
}
else { // the user has submitted the form
include("class.phpmailer.php"); //you have to upload class files "class.phpmailer.php" and "class.smtp.php"
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = $_POST["host"];
$mail->Username = $_POST["uname"];
$mail->Password = $_POST["pass"];
$mail->From = $_POST["uname"];
$mail->FromName = "demouser";
$mail->AddAddress($_POST["to"],"test");
$mail->Subject = "This is the subject";
$mail->Body = "This is a sample message using SMTP authentication";
$mail->WordWrap = 50;
$mail->IsHTML(true);
$str1= "gmail.com";
$str2=strtolower($_POST["uname"]);
If(strstr($str2,$str1))
{
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo "<br><br> * Please double check the user name and password to confirm that both of them are correct. <br><br>";
echo "* If you are the first time to use gmail smtp to send email, please refer to this link :http://www.smarterasp.net/support/kb/a1546/send-email-from-gmail-with-smtp-authentication-but-got-5_5_1-authentication-required-error.aspx?KBSearchID=137388";
}
else {
echo "Message has been sent";
}
}
else{
$mail->Port = 25;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo "<br><BR>* Please double check the user name and password to confirm that both of them are correct. <br>";
}
else {
echo "Message has been sent";
}
}
}
?>
Any info would be great, thanks in advance!
You need to add the true smtp gmail so you can add :
smtp-relay.gmail.com smtp.gmail.com aspmx.l.google.com
Setting requirement
Options : port 25, 465 ou 587
Protocoles SSL/TLS (Secure Socket Layer/Transport Layer Security)
Port 465 (SSL require).
Port 587 (TLS require).
Adresses dynamic IP allowed.
Port 25.
TLS no require.
Adresses dynamic IP allowed.
Looks like i had it set up correctly, it just took a few hours for hostbuddy to get the smtp server set up. Works great now :) thanks for the suggestions!
I am a newbie in PHP, so all i know is actually from the forums. These are the settings i made in my php.ini file
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = engr.atiq19#gmail.com
sendmail_path = "\"C:\xamppnew\sendmail\sendmail.exe\" -t"
;sendmail_path = "C:\xamppnew\mailtodisk\mailtodisk.exe"
These are the changes made in sendmail.ini file
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=engr.atiq19#gmail.com
auth_password=************
force_sender=engr.atiq19#gmail.com
And here is the code I am using to send the mail
$to = "engr.atiq19#gmail.com";
$myemail = "engr.atiq19#gmail.com";
$email_subject = "Contact form submission: $name";
$email_body = "my message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
header('Location: ../index-alt2.html?t="done successfully"');
I would recommend using PHPMailer to send email from PHP. Here's the steps to accomplish this.
Go to the Github repository.
Download the ZIP.
Extract it in your public_html directory.
include '/path/to/PHPMailer/PHPMailerAutoload.php'; at the top of your PHP script.
Get the values from the HTML form like you normally would.
Here's an example...
index.html
<form action="index.php" method="post">
<input type="email" name="email">
<input type="text" name="name">
<input type="text" name="subject">
<input type="text" name="message">
</form>
index.php
include '/path/to/PHPMailer/PHPMailerAutoload.php';
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // 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('your email', 'your name'); // from
$mail->addAddress($email, $name); // to
$mail->isHTML(true); // if html
$mail->Subject = $subject;
$mail->Body = $message; //HTML
if($mail->send()){
echo 'Mail sent!';
}
else {
echo 'Mail failed!';
}
Hi I have this PHP script that I found on a blog
<?php
if(isset($_POST['submit'])) {
$to = "youremail#gmail.com";
$subject = "Forms";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>
Which is run when the following is executed in HTML
<form method="POST" action="mailer.php">
Your Name<br>
<input type="text" name="name" size="19"><br>
<br>
Your Email<br>
<input type="text" name="email" size="19"><br>
<br>
Message<br>
<textarea rows="9" name="message" cols="30"></textarea>
<br>
<br>
<input type="submit" value="Submit" id="submitBTN" name="submit">
</form>
According to the blog all i have to do is put the html and php files onto my web server (Which i don't have so i can't test this). Will it send an email to the email specified in $to ? I've never used PHP but this doesn't really makes sense how it can just email someone once its on the web. Thanks for an explanation/if this script would work straight up!
This should theoretically work, but you will have to configure PHP to use your email server. I would recommend using something like PHPMailer to send email, that is what I always do. PHPMailer allows you to specify your IMAP/POP3 email server host, username & password, and email port, in the same manner that your email client does.
Here is a link to information about using Gmail in PHPMailer.
This snippet (taken from the PHPMailer readme) shows how to configure your server:
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.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
Now, you can configure header information for where the email is from, and who it is going to:
$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');
You can even add attachments to the email:
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
Finally, we put the subject and body into the email:
$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';
Now, we send the email:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You will need to setup a mail server on your server in order to send emails.
Not sure where you need to specify this though for PHP to use..
Take a look here: php mail setup in xampp
(Xampp mentioned in the link is a program that acts as the server so that people can look at your webpages online)
Xampp: https://www.apachefriends.org/index.html
I did the same task long time ago.
I accomplished this trough XAMPP (php and apache) and gmail.
Here you can find a video with the complete explanation.
I have used following settings in joomla -adminpanel- global configuration-server
Mailer :Sendmail
Mail from :user
From Name :sales#user.com
Sendmail Path :/usr/sbin/sendmail
SMTP Authentication :No
SMTP Security :none
SMTP Port :25
SMTP Username:
SMTP Password :
SMTP Host :localhost
I have used form in site
<form action="email.php" method="post" name="emailForm" id="emailForm" class="form- validate">
in email.php i used php mailer as follows
<?php
require_once("class.phpmailer.php");
$mail = new PHPMailer();$mail->CharSet = 'UTF-8';
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$message = $_REQUEST['text'] ;
// Enable encryption, 'ssl' also accepted
$mail->From = 'sales#example.com ';
$mail->FromName = 'Techzo';
$mail->addAddress('ccccc#example.in'); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Enquiry /Contact form';
$mail->Body = 'Name: $name\nEmail: $email\n\n$message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
I have searched and worked accordingly but not in use.As am new to these coding please help me to understand this issue
This happens when sendmail is not installed on your server (sendmail should be under /usr/sbin ). You will need to install sendmail or use an alternative, such as gmail (you can use gmail using the phpmailer class.
Thanks for all your suggestions.I have sorted out the problem a day before.
Problem is sendmail option is not available in that server, I have tried for SMTP with wrong port no(25) previously.587 is port no for the mail server.
BY USING SMTP with all their configuration settings i done through it.
I try to send email from wampserver "localhost" by this php code
<?php
$to = 'test1#gmail.com';
$subject = 'Fake sendmail test';
$message = 'If we can read this, it means that our fake Sendmail setup works!';
$headers = 'From: test2#gmail.com' . "\r\n" .
'Reply-To: eng.jirjawi#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Email sent successfully!';
} else {
die('Failure: Email was not sent!');
}
?>
I used the file "sendmail" and put it inside the file "C:\wamp\sendmail" i changed setting sendmail.ini file to -->
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
default_domain=localhost
error_logfile=error.log
auth_username=test2#gmail.com
auth_password=xxxxxxx
pop3_server=
pop3_username=
pop3_password=
force_sender=
force_recipient=
hostname=localhost
i changed setting php.ini file in C:\wamp\bin\apache\apache2.2.22\bin
and
i changed setting php.ini file in C:\wamp\bin\php\php5.4.3
to
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP =
; http://php.net/smtp-port
;smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you#yourdomain
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
and i changed setting appach module "SSL module" checked
and i changed setting php extension "php openssl" & "php sockets"
And it did not work with the code did not send email
Discussed a lot in the internet and tried many ways
Why does not happen transmitter and what the correct way
Apologized for the mistakes my English language
Please help and thank you
PHPMailer has always worked for me on development servers (localhost), as well as on live websites.
Download PHPMailer here, unzip it and put it in your application's directory.
When you want to send a mail, use the following code :
require 'PHPMailerAutoload.php'; //Your path to PHPMailer's directory
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port to gmail's port
$Mail->Username = 'yourusername#gmail.com'; // gmail account username
$Mail->Password = 'yourpassword'; // gmail account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'test2#gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName = 'Test';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->addAddress('test1#gmail.com'); // To: test1#gmail.com
$Mail->isHTML( TRUE );
$Mail->Body = '<b>This is a test mail</b>';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
Update : You should update your PHP installation for this to work correctly.
For the error you're getting, try this :
In your php.ini search for line
; extension=php_openssl.dll
and remove ; so it becomes:
extension=php_openssl.dll
salem alaykùm (hey)
check this it works for me,
<?php
if ($_SERVER["REQUEST_METHOD"] == 'POST'){
//print_r($_POST);
if (mail('name#example.com', 'New Test Email', htmlspecialchars($_POST['msg']))){
$cas = "thanks for the msg";
}
}
?>
<!doctype html>
<html>
<head> <title>my test company</title>
</head>
<body>
<h3>Contactez nous</h3>
<form action="" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
</li>
<li>
<label for="email">Email :</label>
<input type="text" name="email" id="email">
</li>
<li>
<label for="msg">Message:</label><br />
<textarea name="msg" id="msg"></textarea>
</li>
<li>
<input type="submit" value="Go!">
</li>
</ul>
</form>
<?php
if (isset($cas)) echo $cas;
?>
</body>
</html>