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, ...)
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am about to write a little html/php script. But I am not able to send data from the HTML form input to any email address or into any text file. I was already searching for possible solutions but nothing worked for me. The Browser replayed the php script. But no mail has been send. Any help will be appreciated. Thank you.
I recommend you use PHPMailer his use is very easy
Do not forget the action and method attributes in the <form> tag.
contents of html file
<form action="send.php" method="POST">
<input type="text" name="name" placeholder="Typ your name..." />
<input type="email" name="from" placeholder="Typ your e-mailaddress..." />
<textarea name="message" placeholder="Typ your message..."></textarea>
<button type="submit">Send E-mail</button>
</form>
contents of send.php
<?
if(isset($_POST)) {
$name = $_POST['name'];
$message = $_POST['message'];
$from = $_POSST['from'];
if(!empty($name) && !empty($message) {
$subject = 'message from '.$name;
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
//$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
// #SEE: http://php.net/manual/en/function.mail.php
if(mail('[YOUR-ADDRESS]', $subject, $message, $headers)) {
echo 'Thx 4 msg!';
}
else {
echo 'Oh nooos, The msg was not send.';
}
}
else {
echo 'You should provide the fields with some data..';
}
}
?>
One should first sanitize the user input obviously.
I think your issue is how to handle form data with some code, so you may be able to send an email or write form data to a file. This is where you see the difference between client-side and server-side. HTML is a language to describe documents, here your form: the text input name is going to describe a name, the form will send its data within the POST method, and so on. The file describing HTML is processed in your browser. And your browser don't send an email or write data... That's why you should use a server-side language such as PHP to get things done. PHP is great to help you process data and behave on different event... In your case, great for receiving data, analyze data and then send data through mails or save data into a file.
So now you may want to understand how to do so... Mail are a little tricky since you may need to configure things such as mail server, authentification and so one. Maybe a good solution is to try mails with a Google account or something like that... When it will be done, you may simply send an email like so:
<?php
$to = 'your#email.here';
$subject = 'Mail test';
$data = $_POST['name']; // if a `name` field exist and your form send its data through POST method
mail($to, $subject, $data);
Writing things to a file is simpler, it only request permissions to read and/or write a file.
<?php
$file = 'path/to/file';
file_put_contents($file, $_POST['name'] . ' for example');
So this is globally everything:
index.html HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Form</title>
</head>
<body>
<form action="process.php" method="post">
<input name="name" type="text" placeholder="Name" />
<input type="submit" value="Process">
</form>
</body>
</html>
and process.php PHP file
<?php
/**
* Testing data
*/
if (!isset($_POST['name'])) {
die('No value `name` found');
}
/**
* Configuring process
*/
$to = 'your#email.here';
$subject = 'Mail test';
$data = $_POST['name'];
/**
* Saving data
*/
$res = file_put_contents(
'data.txt',
$data."\r\n"
);
if ($res !== false) {
echo 'data saved'.PHP_EOL;
} else {
echo 'error while saving data'.PHP_EOL;
}
/**
* Sending email
*/
$res = mail(
$to,
$subject,
$data
);
if ($res === true) {
echo 'mail sent';
} else {
echo 'error while sending mail'.PHP_EOL;
}
I suggest you to read mail() and file_put_contents() documentations to understand their behavior in case there are errors... :)
I'm using the php mail function and I have no problem sending the emails, I recieve well the emails on Hotmail and Gmail. But, when I wrote a web address in the message Gmail doesn't accepts the email because it arrives with an hyperlink, even though i'm writing the address in this way: "www.something.com" and I'm using Content-Type: text/plain; on the header.
I've tried to use strip_tags() with the message, but the problem persist.
What can I do?
Please try the code as follow:
<?php
$to = "to_email addrss";
$subject = "Your Subject";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<a href='http://yourlink.com'>http://yourlink.com</a>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#yoursite>' . "\r\n";
mail($to,$subject,$message,$headers);
?>
May be you failed to add content-type:text/html ,while sending your email address. may be this is the reason your anchor link it not working.hope this will work.
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 ";
}
?>
I'm not very good at php so this should (hopefully) be easy for one of you.
I'm trying to send an HTML email from php which includes variables. I'm able to send a none html email with variables, or an HTML email without variables, but I can't seem to do both at the same time. Here's my code:
<?
$to = "me#something.co.uk";
$from = "m2#something.com";
$subject = "Hello! This is HTML email";
'
//begin of HTML message
$message = <<<EOF;
<html>
<p>Opt out of contact by phone<br />
'. $_POST["Opt_out_phone"] .'
</p>
</html> EOF;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
echo "Message has been sent....!"; ?>
I've never come across <<< EOF before and have no idea what it's doing.
You're using HEREDOC syntax and attempting to terminate the string and append the variable incorrectly and unnecessarily. To declare your string with the variable, do this:
$message = <<<EOF;
<html>
<p>Opt out of contact by phone<br />
{$_POST["Opt_out_phone"]}
</p>
</html> EOF;
Note that the PHP manual entry on heredoc syntax is with the rest of the string docs, here.
I have a php script that sends an email. It looks like this:
<?php
// subject
$subject = "$first_name $last_name has sent you a message on The Red-line";
// message
$message = "<html>
<head>
<title>
The Red-line
</title>
</head>
<body>
<p>
Hi $war_first,
</p> <br />
<p>
$first_name $last_name has sent you a message on the Red-line. To view your message, please login to <a href='www.thered-line.com'>the Red-line.</a>
</p> <br />
<p>
Sincerely,
</p> <br />
<p>
The Red-line Operator
</p>
</body>
</html>";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "From: The Red-line messages#theredline.com \r\n";
$headers .= "To: $war_first $last_war <$war_mail>\r\n";
// Mail it
mail($war_mail, $subject, $message, $headers);
?>
When I was testing this out on the remote server, I used this script to send an email to my inbox at turbocapitalist1987#yahoo.com. I got the email but the "from" part doesn't work . Yahoo says that my email was sent from "the#yahoo.com"
Does anybody have a clue as to why this isn't working?
Thanks
For your From header, enclose the actual address with < >:
$headers .= "From: The Red-line <messages#theredline.com> \r\n";
This is the correct format to use when you have both a display name and an email address. I'm guessing Yahoo was interpreting the first word "The" as the entire email address, and provided a default (yahoo.com) for the domain.
The <> seems to be your problem, it should be:
$headers .= "From: The Red-line <messages#theredline.com> \r\n";
Secondly, on Unix/Linux, you must have a working MTA (i.e.: sendmail, postfix, etc.) configured on your server, either to relay mail directly, or through a Smart Host. If you're on Windows, you must have a SMTP server configured in php.ini.
I would recommend using pre-built PHP email library such as: http://swiftmailer.org/