How do I format the information so when it's displayed in the Email client, it'll show line breaks and other html tags that' i've set in the .php file?
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sentMail = #mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
would it be something like:
$message = '<b>$user_Message';
Related
Im trying to write a simple script to send a message to .csv list of e-mail addresses like this:
mail1#mail.com,
mail2#mail.com,
mail3#mail.com,
Here is what I have tried:
<?
$csv = array();
$file = fopen('test.csv', 'r');
while (($result = fgetcsv($file)) !== false){
$csv[] = $result;
$to = $result[0];
$subject = "My mail subject";
$message .= '<html><body>';
$message .= 'My message here';
$message .= "</body></html>";
$from = "example#example.com";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
}
fclose($file);
?>
But the message is sent three times, and includes the message three times in the body. How can I make the message only be included and sent one time to each line?
You should remove $subject, $message, $from and $headers definition from the while loop. Define all of them before and in the while define only the $to field and keep the mail() command.
Either follow the above suggestion (good approach) or simply remove concatenation operator "." form first assignment to variable $message i.e. change
$message .= '<html><body>'; TO
$message = '<html><body>';
This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 9 years ago.
I'm sending an email via PHP's mail() function. In the message I set a link that looks like this:
$message = "<a href='". $link. "'>" .$title. "</a>\n\n";
However, when the email is received, the email body shows the html code instead of the title as a hyperlink. I'm not very familiar with html emails. How could I achieve what I am trying to get?
Try to add an header, so that the mail client doesn't believe it is plain text:
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
See PHP mail function manual:
Example #4 Sending HTML email:
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</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 .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Please see the documentation for PHP mail() at http://php.net/manual/en/function.mail.php
You need to specify a Content Type of HTML in your function.
// 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 .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
Although it's generally recommended to avoid using mail() alone.
You should consider using PHPMailer, for example.
You have to tell the e-mail client that there is an HTML portion to the e-mail. I'd recommend something like Swiftmailer to do the work instead of doing everything yourself.
Is there a possible way, to insert data from table to mail sending script? I have made this simple script, but it doesnt work. How to mix these 2 codes?
$result = mysql_query("SELECT * FROM tablename WHERE ID =1" ) or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo ''. $row['maillist'] .''; }
$to = 'here must be maillist row';
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
mail($to, $subject, $message, $headers);
My second question is: If i am using bcc, than gmail or other mail services displaying full code of message with all tags, but not displaying image. So, is there a possible way to fix this problem?
My third question is: If i am inserting image to message (watch the code), then the message appears in SPAM, but if i am using only basic text, than its all normal. How to fix it?
I will be grateful for any answers and help!
$q = "SELECT email FROM table WHERE id = '" . $id . "'";
$r = mysql_query($q) or die(mysql_error().'<br />'.$q);
$d = mysql_fetch_assoc($r);
$to = $d['email']:
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
mail($to, $subject, $message, $headers);
If you wanna send email to several people :
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
$q = "SELECT email FROM table WHERE id = IN (" . $array_ids . ")";
$r = mysql_query($q) or die(mysql_error().'<br />'.$q);
while($row = mysql_fetch_assoc($r)) {
$to = $row['email']:
mail($to, $subject, $message, $headers);
}
For the SPAM problem, I'd say try to set you header like this
$headers .= 'Content-type: image/jpeg' . "\r\n";
I've configured exim on my server as MTA to work with gmail.
Here is a configuration:
gmail_login:
driver = plaintext
public_name = LOGIN
client_send = : myaccount1#gmail.com : mypassword
The configuration is OK and I'm able to send a mail using a php script:
$to = 'myaccount3#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: myaccount2#gmail.com' . "\r\n" .
'Reply-To: myaccount2#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'mail() Success!' . "<br />\n";
}
else {
echo 'mail() Failure!' . "<br />\n";
}
However I've encountered an issue:
gmail shows myaccount1#gmail.com in the FROM field instead of the actual email specified in the FROM field in my script (myaccount2#gmail.com).
The reply-to field is OK.
Please, help to solve the issue.
Gmail overwrites any FROM value you specify. Gmail overwrites it with the authenticated FROM value.
I'm having a textarea in my contact form with php mail function. In php mail i have set the header to html.
But even if the user types like this
Line1....
Line2....
I'm getting in mail like this.
Line1....Line2....
What could be the reason?
Update:
The text area is as simple as this.
<textarea id ="msg" name="message" cols="" rows="5" class="msg">Message</textarea>
Its posted to this script with jquery ajax function
<?php
$sub = "Message Posted";
$email = "some#somewhere.com";
$message = "<b>Message :</b><br/>" . $_REQUEST["msg"];
$message = wordwrap($message, 70);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ifthi#ifthi.com' . "\r\n" .
'Reply-To: '. $_REQUEST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail($email, $sub, $message,$headers);
?>
But when getting in email all are in a single line. Even if you write in 2 line and submit.
if you set your mail to HTML you should replace all line breaks with HTML Tags.
I think the PHP function you need is:
string nl2br ( string $string [, bool $is_xhtml = true ] )
<?php
//whatever you want to replace new lines with
$newLineCode = "<br/>";
$message = $_POST['myTextArea'] ; //unadulterad text we got via Post
$modifiedTextAreaText = ereg_replace( "\n", $newLineCode, $message);
echo " Result of Code Snippet " . $modifiedTextAreaText ;
?>