I've looked at the documentation and here's how I'm sending my email:
$headers = 'From: aaaaaa#aaa.com' . "\r\n" .
'Reply-To: aaaa#aaa.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail('aaaaa'aaaa.com', 'Contact: ' . $name . ' from ' .$company, $message, $header);
Where $message is a string containing several <br /> tags for line breaks.
I've set the Content-type to text/html so I would expect the email to arrive as HTML, but in my inbox it's clearly interpreted as just text (IE: I just see the text of my <br /> tags)
It's this
$headers = 'From: email#example.com' . "\r\n" .
^ - notice the s?
$company, $message, $header )
^ missing the s
Related
I would like to send an Email with PHP mail function but my HTML is being sent as plain text.
This is my code:
$from = 'admin#bla.com';
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$message = file_get_contents('cancelemail.php'); //Includes my HTML Page
thanks!
your headers should be array like:
$headers = array("Content-Type: text/html; charset=UTF-8", "From: $from ");
Also, there is no point setting content as php file, content inside will be read as html, the php code will not be executed;
I am trying to send emails with HTML content and whenever i pass headers of any kind the method mail() return false but with no headers i get true .. any clue ?
$to = $user->email;
$name = $user->first_name.' '.$user->last_name;
$from = $this->config->item('admin_email', 'ion_auth');
$subject = $this->config->item('site_title', 'ion_auth') . ' - ' . $this->lang->line('email_forgotten_password_subject');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval = mail ($to,$subject,$message,$headers);
die(var_dump($retval));
Make sure that variables $name, $from, $to, $subject has the right data and are not empty/null. Your code is functional and should send the email.
If still doesn't work, check the configurations on postfix or what other mail server you're using.
And I suggest to use PhpMailer or SwiftMailer instead of simple mail function.
Try adding a CRLF after your last header. Used to work for me in the olden days.
i.e.,
...
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n";
...
Also, the header Content-Type has a capital T. Sometimes, such trivial things may also break it. That's why everyone suggests to move away from it.
That should do it.
Suggestion: Switch to PHPMailer.
I am trying to send few emails and ofcourse I need to breake some lines and put some spaces.
This is how I'm trying to do it, without luck:
for ($i = 0; $i < count($dataArray); $i++) {
$to = $dataArray[$i]['email'];
$subject = 'New member message';
$message = "Hello!" . "\r\n" . "Member message: " . "\r\n" . "\r\n" . $dataArray[$i]['message'] . "\r\n" . "\r\n" . "This is an automated message, please do not respond to it!";
$headers = 'From: info#domain.com' . "\r\n" .
'Reply-To: info#domain.com' . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
But the result leads to a single line text, including the \r\n in it as text - it is not inserting a new line.
How can I insert a new line? Why \r\n is not working?
I know that I'm missing a really, really small part here, but as a php beginner, I'm not able to spot it.
For Content-type: text/html, new line is <br>
For Content-type: text/plain, new line is \r\n
Try <br> tag before /r/n. hope it will work.
You have to use a simple
<br />
for a new line, the
\r\n
is just for the header
Try this code.
for ($i = 0; $i < count($dataArray); $i++) {
$to = $dataArray[$i]['email'];
$subject = 'Testing sendmail.exe';
$message = "Hello!" . "\r\n" . "Member message: " . "\r\n" . "\r\n" . $dataArray[$i]['message'] . "\r\n" . "\r\n" . "This is an automated message, please do not respond to it!";
$headers = 'From: info#domain.com' . "\r\n" .
'Reply-To: info#domain.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers))
{
echo 'mail send';
}
else
{
echo 'mail fail';
}
}
Change content type of header to 'Content-type: text/html; charset=iso-8859-1'
I am trying this on wordpress, trying to email multiple email address. I have 2 email addresses in the database.
Here is the code:
$r = $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_users", $ID, $user_email ));
foreach ($r as $row) {
$to = 'someone#myhost.com';
$bcc = $row->user_email;
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo "Email sent";
}
else {
echo "Email sending failed";
}
It's sending emails BUT what's happening is that the TO (someone#myhost.com) is getting 2 emails and the $bcc is getting none.
What am I doing wrong here?
Yep, this behavior it is pretty normal, you forgot to put in $headers the Bcc: part, it should be like:
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Bcc: '.$bcc. "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
I want to send email using PHP when an HTML form is submitted.
I learnt from a tutorial and made this PHP script, but when I check it in my webmail, I only see email address, subject and message, but no name. What do I need to do to get the name to show up?
Form variables:
if (empty($_POST) === false) {
$errors = array();
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
// ... etc (validation)
if (empty($errors) === true) {
$headers = 'From: '.$email. "\r\n" . $name.
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('mail#example.com',$subject,$message,$headers);
print "<p class='formerrors'>Thank you for your message, I'll get back to you shortly!</p>";
}
The format of your "From" header isn't right. It should be in the following Format:
"From: Sender Name <sender#domain.com>"
So your $headers assignment should read:
$headers = 'From: '.$name.' <'.$email. ">\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You should modify your $headers variable as:
$headers = 'From: ' .$name. ' <' .$email. '>' . "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Please read more about mail function on php.net over here: http://php.net/manual/en/ref.mail.php
Moreover maybe PHPMailer, see https://github.com/PHPMailer/PHPMailer is interesting for you to enrich your mails.