Can entire emails be composed and sent with a PHP command? - php

I am following this guide:
http://www.w3schools.com/php/php_mail.asp
So by following this guide can I really send an email to myself (someemail#website.com) by opening this page I called "email.php" in Chrome or other internet browser?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1> Email Application </h1>
<?php
$to = "someemail#website.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "Some Guy";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
</body>
</html>
I tried running email.php seen above locally but nothing happens. Do I need to put email.php on a server?
I'm currently working on a Contact Form and I want all the information entered in a contact form's fields to be sent to a specified email address. I'm doing this for a company's website.

Yes, PHP can send e-mails. But you need to configure a mail (SMTP) server in php.ini.
If you have a mail server running locally, php.ini is set to use it by default. Don't forget to turn on the mail server and configure it to allow relaying of local mails.
I you have a remote mail server (you can even use Google's SMTP server), set php.ini to use it.
You might want to change
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
into
if (mail($to,$subject,$message,$headers))
echo "Mail Sent.";
else
echo "Failed sending e-mail";

This happened due to your smtp setup and not your code. You should verify that your smtp is properly set up.
Try using
echo ini_get("SMTP");
echo ini_get("smtp_port");
To get your SMTP details. Check your php.ini
It should be in this format
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
<?php
$to = "something#email.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: XYZ <xyz.com>\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Mail Sent.";
}
else
{
echo "Mail Failed ";
}
?>

Related

PHP mail not working and returns false

php mail return false when i run mail function
here is my code
$to = 'xxxxxx#gmail.com';
$subject = 'Login';
$message = 'Thanks for Login ! Your account has been logged on website.';
$headers = 'From:xxxxx#gmail.com' . "\r\n";
var_dump(mail($to, $subject, $message, $headers));
when i opened mail log there is no error found and when i test to send mail over ubuntu terminal using this command
echo "Test mail from postfix" | mail -s "Test Postfix" you#example.com
it sends to me mail correctly
also i configured php.ini
sendmail_path = /usr/sbin/sendmail -t -i -f care#mydomain.com
first try this one: use this code make a Connection
<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
$to = "xyz#somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header .= "Cc:afgh#somedomain.com \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...";
}
?>
</body>
</html>
I had this issue, but in my case it was due to my postfix configuration being quite restrictive. I found this error in /var/log/maillog:
postfix/sendmail[pid]: fatal: User apache(id) is not allowed to submit mail
The fix was to add "apache" to the main.cf:
authorized_submit_users = root spamd apache
Then, don't forget to restart postfix
systemctl restart postfix

Few PHP script E-Mails Not getting delivered

I have created folders under my domain.
Emails scripts in folder-1 are getting delivered but scripts in folder-2 show message = "email sent successfully". But these emails are not receiving in email address.
I am using phpmailer
When this sampleemail.php file is kept in Folder1, Email get delivered. But when same file is kept in Folder2, Error Message is there.
Code is as follows :
<?
$msg="";
if(isset($_POST['submit']))
{
$from_add = "name#my-web-site.com";
$to_add = "myemail#gmail.com"; //<-- put your yahoo/gmail email address here
$subject = "Test Subject";
$message = "Test Message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent OK";
}
else
{
$msg = "Error sending email!";
}
}
?>
And HTML Sample Form As Follows :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test form to email</title>
</head>
<body>
<?php echo $msg ?>
<p>
<form action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>' method='post'>
<input type='submit' name='submit' value='Submit'>
</form>
</p>
</body>
</html>
I would suggest you use PHPmailer because it has the function which you needed. The reason i am not suggest you use your method because it need to change the sendmail() function in your localhost.
For example: if u using xampp, you must go into the sendmail directory and go for sendmail.php to edit the SMTP to mail.google.com and so on.
Please try this tutorial:
http://codeforgeek.com/2014/11/phpmailer-ultimate-tutorial/
Hope it can helps you.
Looking at your code I see you are NOT using "PHPMailer", but "PHP's mailer".
Regarding your problem of undelivered emails and error message, here are some general hints:
If you send HTML mail, always send a multipart email containing both, HTML and TEXT
Don't use "X-Mailer: PHP". There are mail servers who will give your mail an increased spam score just because of that.
If you get an error, try this:
$errLevel = error_reporting(E_ALL ^ E_NOTICE);
mail(...);
error_reporting($errLevel);
Try using a different delivery method (smpt, sendmail, ...)

Why is this PHP script failing to send HTML mail?

I have this piece of test code to send an HTML email. For the longest time, it was sending, but not sending in HTML format. And now, it's not sending at all.
I've been comparing with code on the PHP manual, and with similar questions on this site, and I can't see anywhere that the code could be wrong. It's driving me crazy.
Why is this code not sending, and why is it not sending in HTML?
$to = "myemail#mysite.com";
$subject = "Confirmation code for registration";
$message = "<html>
<head>";
$message .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
$message .= "<title>TITLE</title>
</head>
<body>";
$message .= "Thank you for registering! \r\n";
$message .= "
</body>
</html>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf-8\r\n";
$headers .= "From: My Site <registration#mysite.com>r\n";
$mailresult = mail($to, $subject, $message, $headers);
echo "mail sent! ";
echo $mailresult;
$mailresult comes back as 1, which I assume means success, but no mail is being recieved at the mail account specified.
According to php.net:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
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.
Have you configured a mail server to send the mail?
Your code worked on my server perfectly but I received the email as Spam - so check your spam. And do your research to check if your server is configured properly.
Try doing
mail($to, $subject, $message, $headers);
without $mailresult

send email with php in a local host

I want to send email with php using xampp.
<html>
<head>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$to = $_POST['email_add'];
//define the subject of the email
$subject = $_POST['sbjct'];
//define the message to be sent. Each line should be separated with \n
$message = $_POST['msg'];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$mail_sent = #mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
<form action="send_email.php" method="post">
Email Add<input type="text" name="email_add" />
Subject<input type="text" name="sbjct" /> <br>
<textarea name="msg" rows="9" cols="20"></textarea><br>
<input type="submit" value="Send"/>
</form>
</body>
</html>
It's not working. Is there something wrong with my code?
I found some tutorials on the cloud, but it mentions SMTP and I do not understand it.
PHP doesn't actually send the email... in Linux it'll forward it to sendmail or similar daemon, in Windows it'll forward it to an SMTP server. Depending on your OS you have to setup these other processes and explain to PHP how/where to contact them (in php.ini in the [mail function] section).
PHP need a SMTP server to send emails.
You can specify it in your php config file (and set it to your ISP one for example), or use "Test Mail Server Tool" ( http://www.toolheap.com/test-mail-server-tool/ ) that catches the smtp local calls and saves them in a directory of your hard disk, so that you can debug everything.
Obviously, this solution works only if you need to debug it.
If your need is to send the mail, please refer to php documentation and use your ISP SMTP server.
If you are using the mail() function on Windows from a local development environment, you may need to specify an SMTP server that can relay the mail for you. Linux has this functionality baked in, Windows may not. Your ISP may provide you with this or a webmail provider may allow you to relay mail through them.
Look inside your php.ini file and locate the [mail] section and fill in:
SMTP = smtp.yourprovider.com
SMTP_PORT = 25
From the PHP mail manpage
"The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine)."
I think you're looking for http://www.php.net/manual/en/function.mail.php ?
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Email Add<input type="text" name="email_add" />
Subject<input type="text" name="sbjct" /> <br>
<textarea name="msg" rows="9" cols="20"></textarea><br>
<input type="submit" name="submit" value="Send"/>
</form>
</body>
</html>
<?php
If(isset('submit'))
{
error_reporting(E_ALL);
$to = $_POST['email_add'];
//define the subject of the email
$subject = $_POST['sbjct'];
//define the message to be sent. Each line should be separated with \n
$message = $_POST['msg'];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$mail_sent = mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
}
?>
Here is a script I re-use in a lot of my projects, pretty straight forward just copy in and change the obvious values.
$email_to = "myemail#gmail.com,anotheremail#gmail.com";
$email_subject = "This is the subject line";
$break = 'echo "/n"';
$form_email = "no-reply#myemail.com";
//Function to convert /n to line breaks in HTML
function nl2br2($email_message) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $email_message;
}
//Unformatted email body
$email_message = "This is the main blody of the email";
//Format string against function
nl2br2($email_message);
// create email headers
$headers = 'From: '.$form_email."\r\n".
'Reply-To: '.$form_email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);

Send email using domain server email with PHP

.hi guys can anyone guide me on how to create a PHP form that sends an email to a desired recipient using the email provided by my domain server.
<?php
if($_POST['submit'])
{
$name = $_POST['name'];
$message = $_POST['message'];
if($name&&$message)
{
$namelen = 20;
$messagelen = 300;
if(strlen($name)<=$namelen&&strlen($message)<=$messagelen)
{
$to = "myemail#yahoo.com";
$subject = "Test Email";
$headers = "From: my server provided email here";
ini_set("SMTP", "/*i placed my domain server here");
$body = "This is an email from $name\n\n$message";
mail($to, $subject, $body, $headers);
die();
}
else
die("Max length for name is $namelen, and max length for message is $messagelen.");
}
else
die("You must enter a name <u>and</u> message");
/*echo $name.' '.$message;*/
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action='mailpush.php' method='POST'>
Name:<input type='text' name='name' maxlength='20'><br>
Message:<br /><textarea name='message'></textarea><p>
<input type='submit' name='submit' value='Send me this'></p>
</form>
</body>
</html>
.this is the code i have so far. but when i try to send i don't receive anything.
when i try to send i don't receive anything
So really your problem is not in writing the code, but in diagnosing why it's failing.
First thing of note is that you don't check the return value of the mail() call.
ini_set("SMTP", "/*i placed my domain server here");
What is this supposed to mean? There is no such thing as a "domain server". There are domain name servers, SMB domain masters, SMTP servers.....
Next, you've provided no details of the OS this is running on nor the config for mail in php.ini : although you're explicitly setting the SMTP host (to what?, is it resolvable?) what is the setting for smtp_port? Can you telnet to that port on the named machine from where the PHP code is running?
There are an awful lot of technology things between your code and your mailbox - many of which could cause problems with the mail delivery - have you looked at these? Your local SMTP server is just the next hop in the chain, did you check if your email was enqueued there? If it was, then its nothing to do with PHP.

Categories