I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:
$to = \"myweb@gmail.co
Here's the link that gives me the answer and we use gmail:
Install the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip
Modify the php.ini file to use it (commented out the other lines):
For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 25
For Win32 only.
sendmail_from = <e-mail username>@gmail.com
For Unix only.
You may supply arguments as well (default: sendmail -t -i
).
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
(ignore the "Unix only" bit, since we actually are using sendmail)
You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:
smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com
Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer
How to use it:
require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'me.sender@gmail.com';
$mail->Password = '123!@#';
$mail->SetFrom('me.sender@gmail.com', $name);
$mail->AddReplyTo('no-reply@mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1@gmail.com', 'title1');
$mail->AddAddress('abc2@gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();
configure your php.ini like this
SMTP = smtp.gmail.com
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = smtp.gmail.com
; smtp_port = 465
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost
You won't be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.
You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.
If you own a server or VPS upload the script and it will work fine.
This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.
<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'your.email@address.com';
$subject = "New Message on YourWebsite.com";
$body = '<html>
<body>
<h2>Title</h2>
<br>
<p>Name:<br>'.$name.'</p>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers = "Reply-To: ".$email."\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=utf-8";
//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
echo '<br>';
echo "Success. Thanks for Your Message.";
} else {
echo 'Error.';
}
}
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Your Name"><br>
<input type="text" name="email" placeholder="Your Email"><br>
<button type="submit">Subscribe</button>
</form>
</body>
</html>