This question already has answers here:
PHP : send mail in localhost
(5 answers)
Closed 8 years ago.
I have been trying to send email using php, I am able to send email when I try to run code on Linux but unable to run it on windows 8, I have XAMPP installed on my windows. Here is the code.
<?php
$msg = "Checking Email";
$msg = wordwrap($msg,70);
mail("myemailaddress","My subject",$msg);
?>
I look online and found out that I have to edit php.ini and sendmail.ini file. I am not sure though that this is the right solution. If it is, can anyone please tell me what exactly do I have to edit or change it, because I tried to edit those files but it still not working.
Thanks
If I were you I would try using PHPMailer. I prefer using it over PHPs built in system. I find it less of a hassle.
Here's some of what you can do with it:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
There is more information on their github page. And there should be no trouble setting it up if you have access to a web server, with email credentials - you would need a host not just XAMPP or LAMP (assuming you intend to get that eventually).
Related
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 6 years ago.
Is there a way I can set the email address used by php's mail() function without getting caught by spam filters?
If I don't add a from header the email is sent from apache#ip-17....3.us-west-2.co....rnal which isn't the worst name because most mail services just shorten to Apache. But I would like to have that be noreply.mydomain.com If I add a from header and put anything else it seems to always get caught by spam but not if just left to default.
My setup is PHP 7 on linux Amazon EC2 Micro
You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer .
It allows you to use the mail function or use an smtp server transparently . It also handles HTML based emails and attachments so you don't have to write your own implementation.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I'm close to finishing my first real website, this website uses php mail function to send mail from my website. My script works because I'm running mercury which routes the mail through gmail smtp. Now my question is if someone can tell me how to connect to this smtp server from php because my webhost doesn't allow me acces to the php.ini file.
Thanks in advance
This PHP class will do it for you. Most real webapps don't rely on PHP's mail(), they use this class instead (or swiftmailer, but his one's much easier).
PHPMailer
Sample code (from the linked page)
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$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
$mail->From = 'from#example.com';
$mail->FromName = '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');
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I am trying to send an email with my server. I am using php's mail function. The function is returning true. But I am not receiving any email. I have checked the logs. And even the logs are not showing any error. My domain is "islamerkotha.com". My code is given below -
<?php
$msg = "First line of text\nSecond line of text";
$msg = wordwrap($msg,70);
$headers = "From: test1#islamerkotha.com";
mail("erfan.bashar.13#gmail.com", "My subject", $msg, $headers);
Thank you.
There are many points along the path an email message takes where yours could be failing, but take a look at the PHP mail() function page; it specifically says that the function returns true if the message was accepted for delivery and "it is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination."
EDIT: Look here for more information on PHP error reporting. If you don't see any errors then, look at phpinfo() to see if the mail() function is even enabled. If it is, then it's time to start looking further downstream...
Try PHP Mailer, with this, you doesn't need to have local SMTP server and also you can also use your validations
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$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
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
My code is this.
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
$to = "myemail#gmail.com";
$subject = "This is subject";
$message = "This is simple text message.";
mail($to,$subject,$message);
echo "<br>Message sent successfully...";
?>
</body>
</html>
I know this is very simple but I am not able to solve it my try to help me
Is there a mailserver running on the machine that hosts this page? Most likely not because the code you show should work.
Now there are two options: Install a mailserver or use an additional PHP library to overcome this problem. I'm not gonna post a tutorial on how to install a mailserver so just Google it!
For the PHP library part, u could use PHPMailer. This class holds additional settings for sending the mail via specific servers. First download the needed files from GitHub (on the right side, click "Download zip") https://github.com/PHPMailer/PHPMailer . Unzip the package and copy the files to your working directory. The code could be like:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$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
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
The easiest way to send mail is to use a gmail account or something. The settings can be found literally everywhere. For example: http://phpmailer.worxware.com/?pg=examplebgmail
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I know HTML & CSS. I am building a website. added a 'contact us' page. I am using free web hosting at this moment which runs a linux server. php mail function is working fine now..
What are the other ways in which an email can be sent automatically through a website on receiving users information?
Thanks in advance..
I suggest PHPMailer , compared with the php function mail() offers many more features
PHPMailer - A full-featured email creation and transfer class for PHP
Class Features
Probably the world's most popular code for sending email from PHP!
Used by many open-source projects: Drupal, SugarCRM, Yii, Joomla! and many more
Integrated SMTP support - send without a local mail server
Send emails with multiple TOs, CCs, BCCs and REPLY-TOs
Multipart/alternative emails for mail clients that do not read HTML email
Support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
SMTP authentication with LOGIN, PLAIN, NTLM and CRAM-MD5 mechanisms over SSL and TLS transports
Native language support
DKIM and S/MIME signing support
Compatible with PHP 5.0 and later
Much more!
Example
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
PHPMailer
Documentation
you can try PHPMailer library to send e-mails via your own SMTP account.
Sample code:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You give very little information about your server hosting environment, but the correct answer is to get the php mail to work. There are so many reasons why, that is is impossible to answer without much more information