I'm still green to PHP and I have built a simple mail() form using php. I have done my best to tidy the code and use the syntaxes and methods as supplied here.
However, I'm still running into some issues, with EOD. I have coded my EOD to send a few basic lines of html, <br/> <hr/> <b/> <em/> etc.. My issue is that the HTML is not being recognised by mail clients such as mac mail, outlook, etc.
I have tried simply using $body = '<html></html>'; and declaring the headers as per the documentation on php. But I am still having the same issue. I have included my basic mail function below; It may be that it's a syntax error of somekind or im missing something else entirely. However, after a day or two of troubleshooting I can't seem to isolate the problem.
Any help would be greatly appreciated.
// mail body.
$body = <<<EOD
<h2>Booking Request / $date</h2>
<hr/><br/>
Last Name: $lnameField<br/>
First Name: $fnameField<br/>
Company: $comField<br/>
Title: $ttlField<br/>
Email: $emaField<br/>
Acitivity: $actSelect<br/>
<br/>
<h2>Contact Info</h2>
<hr><br/>
Add Line 1: $add1Field<br/>
Add Line 2: $add2Field<br/>
Country: $couField<br/>
Telephone: $telField<br/>
<br/>
Requested Booking day: $daySelect<br/>
Requested Booking Time: $selectedTime<br/>
<br/>
Interested in: $selectedProjects.<br/>
Comments: $comms<br/>
submitted: <b>$date</b> at <b>$time</b>.
EOD;
// form submission check.
if (isset($_POST['btn-sub'])) {
// subject & account.
$emailSub = 'Drupa 2016 - Booking Form Actioned';
$emailAcc = 'MGI Technology (SHCP) <testing#test.co.uk>';
// confirmation details.
$confSub = 'Confirmation of booking';
$conf_sender = 'MGI Technology (SHCP) <no-reply#test.co.uk>';
// mail headers
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: $conf_sender";
$headers[] = "X-Mailer: PHP/".phpversion();
// mail info
$success = mail($emailAcc, $emailSub, $body, implode("\r\n", $headers));
$confirm = mail($_POST['ema'], $confSub, $body, implode("\r\n", $headers));
}
// redirect & exit.
header('Location: prox.php');
exit();
Related
I trying to send email using the PHP mail function but when I use fake email and it gives me no error and I cannot find out the email was sent or not I try all the ways from other similar question answers but I don't get what I want
I need to get a delivery report
my PHP version is 7.3
my PHP codes:
$to = 'example#somesite.com';
$subject = 'Message from ';
$message = $Message ;
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'To: '. $to;
$headers[] = 'From: <sombody#theresite.com>';
if(mail($to, $subject, $message, implode("\r\n", $headers))){
echo 555;
}else{
echo 444;
}
Is there any way to get message for successful sent or fail?
For delivery confirmations:
You have to add the Disposition-Notification-To header.
For usage details see RFC 3798.
General tip for stuff like this:
Use the mail client of your choice to generate an e-mail with the desired parameters, send it to yourself and look at the mail source.
There you can find the necessary headers added by the feature you are looking for. Then read the RFC or google for specific details on the header in question.
Ref: Delivery reports and read receipts in PHP mail
I'm sending emails via mail() that include an URL with a variable, that allows recipients to view filtered contents only.
$text_body= "anglebracket_a_href='https://example.com/list.php?var=$variable'">Link anglebracket/a>"
Variables are stored in a mysql database. Everything works fine so far, but the URL displays differently in every mail client.
Instead of the correct version:
<br>
https://www.example.com/list.php?var=76733d141
In Thunderbird it reads
<br>
tps://www.example.com/list.php?varv733d141*
(https truncated! and = sign and the first 2 digits turn into a v)
In Webmail it reads
<br>
https://www.example.com/list.php?varv733d141
In my iPhone mail app it displays correctly and the link is clickable and works!
These are my headers:
$recipient = "test#example.com";
$subject="Your Login Data for $var1 at $var2 in $var3";
$text ="";
$text .=$text_body;
$sender = "Test <noreply#example.com>";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: text/HTML; charset=ISO-8859-1";
$headers[] = "Content-Transfer-Encoding: quoted-printable";
$headers[] = "From: {$sender}";
$headers[] = "Reply-To: {$sender}";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($recipient, $subject, $text,implode("\r\n",$headers));
Tried different encodings and delimiting and everything. Which basic or not so basic problem am I overlooking? Thanks for any help!
Since you say that you're sending quoted-printable, you should actually encode it properly. The = character has special meaning in quoted-printable encoding. Use the quoted_printable_encode() function to encode the body.
mail($recipient, $subject, quoted_printable_encode($text),implode("\r\n",$headers));
Php code which I have used below, its sending blank emails without capturing the values
<?php
$name = $_POST['name'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'test#gmail.com';//replace with your email
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
Edit: (after testing OP's code)
Your headers - mail() parameter are failing you.
I noticed you borrowed an example from the manual on http://php.net/manual/en/function.mail.php
This being your posted code:
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <sender#domain.com>";
$headers[] = "Bcc: JJ Chong <bcc#domain2.com>";
$headers[] = "Reply-To: Recipient Name <receiver#domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
but failed to use the implode() function from the example in the manual, being:
mail($to, $subject, $email, implode("\r\n", $headers));
Had you error reporting set to catch and display, you would have been presented with the following:
Warning: mail() expects parameter 4 to be string, array given in /path/to/file.php on line x
So, you need to modify your last line to read as:
mail($to, $subject, $message, implode("\r\n", $headers));
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
However, the rest of my original answer should also give you more information.
Since you did not provide us with your HTML form, I am submitting the following answer.
As per the manual http://php.net/manual/en/tutorial.forms.php
Example #1 A simple HTML form
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If your HTML form (which you failed to include in your question) does not use a POST method, and/or the inputs do not bear the same name attributes as your POST arrays, then that is the reason why you are receiving blank values, or that your HTML form and PHP are inside the same file and are sending blank values on initial page load.
Sidenote: If your form does not explicitly use a POST method, forms default to a GET method if omitted, another possible reason why it's failing, due to probable undefined index notices you may not be seeing if error reporting is not set on your system and to catch and display errors/notices/warnings.
Therefore, you need to check for empty()'ness.
http://php.net/manual/en/function.empty.php
I.e.:
if(!empty($_POST['var'])) { $name = $_POST['name']; } and applying that logic to the rest of your POST arrays.
Also using a named submit button with a conditional statement wrapped around the executable code.
I.e.: <input type="submit" name="submit" value="SUBMIT">
then if(isset($_POST['submit'])) {...}
while checking if any of the inputs are filled/not empty.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Well I am using the PHP MAIL function and for some reason every email it sends has a weird;
This is at the end of any email as I said, I am not quite sure why this is happening.
$from = 'From: support#phycraft.co.uk';
$to = $user_email; // Send email to our user
$subject = 'PhyCraft Support Ticket :: Closed :: ' . $t_subject; // Give the email a subject
$message = '
Hello '. $username.'.
Your support ticket '.$t_subject.' has been closed due to being inactive for 48 hours.
If you still need help with the ticket please reopen the ticket by replying to it.
~PhyCraft
';
$headers = 'From:support#phycraft.co.uk' . "\r\n"; // Set from headers
mail($to, $subject, $message, $from); // Send our email
I can't see what in the code woud make that appear to be honest.
Most issues with php's mail() function are problems with the MTA and not with PHP itself. I've never heard of this before making it even more likely it's a MTA issue.
You've not provided any useful information beyond the PHP code. What OS is this on (mail() on MSWindows is very different from everyhere else). Do you control the server? What MTA is configured? Have you tried sending an email from the command line?
The extra stuff at the end looks like HTML - is this byte for byte what's in the email or what you see in your mail client?
BTW it's not a good idea to explicitly put "\r\n" at the end of your headers - but you seem to have forgotten to add them as a parameter. Also, your missing a space between "From:" and the email address.
Can you try it with following $headers ( only \n ).
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset = \"ISO-8859-1\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: support#phycraft.co.uk\n";
$headers .= "\n";
mail($to, $subject, $message, $headers);
and 2. try it without
$headers .= "Content-Transfer-Encoding: 8bit\n";
PHP mail sending problem when using a tag, it doesn't come to new line.
HERE is my code having same problem
$subject = 'Watch Out Our Colorful Web Design Presentation';
$headers = "From: " . $email . " \r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "Bcc: test#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "Watch Out Our Colorful Web Design Presentation.\r\n";
$message .= "<a href='http://www.stackoverflow.com'>CLICK HERE</a>\r\n";
mail($to, $subject, $message, $headers);
Mail send successfully but having problem in \r\n. It doesn't take new line. I tried br tag too. But it goes in junk mail.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
You're sending a HTML email. This means that you should be using HTML instead of newlines. To avoid having your emails placed in the junk folder, you should read some of the many Stackoverflow topics on the subject.
You have to use <br> tag for new line.
You can't use \n for new line for html printing.
https://bugs.php.net/bug.php?id=9542
As others have said, you need to be using HTML line breaks <br /> if you are sending an e-mail with Content-Type: text/html. The newlines/carriage returns will be interpreted in the source of the message as line breaks but they will probably not be rendered as HTML.
When sending e-mail from PHP I would always suggest using an e-mail class rather than PHP's native mail functions.
I tend to use SwiftMailer. It has the advantage that all mail headers are sanitized and escaped to avoid header injection exploits that could potentially fire out spam through your script. Also it's easier to use a variety of e-mail transports. There's also a great decorator plugin which can send thousands of messages with customised strings, useful for doing things like "Dear {first_name} {surname}" or customised unsubscribe/tracking links.
Here's some sample code for SwiftMailer just in case you are interested...
// START SWIFTMAILER
require_once($swiftmailer_path);
$swift_transport = Swift_SendmailTransport::newInstance($sendmail_cmd);
$swift = Swift_Mailer::newInstance($swift_transport);
$swift_msg = Swift_Message::newInstance($swift_transport);
$swift_msg->setMaxLineLength(150);
$swift_msg->setFrom( array('NoReply#domain.com' => 'MyWebsiteName'));
$swift_msg->addTo($user);
$swift_msg->setSubject($subject);
$swift_msg->setBody($msg_html, 'text/html');
$swift_msg->addPart($msg_txt, 'text/plain');
// SEND E-MAIL
if ($swift_result = $swift->send($swift_msg)) {
// SENT SUCCESSFULLY
} else {
// ERROR - E-MAIL NOT SENT
}