PHP Mail Using SMTP taking info from HTML form - php

I've recently moved hosts and now with the contact form I have to use SMTP. I have been able with some help get something to work but am unable to retrieve the information I need from the form. The information I am trying to pull is name email phone and message. This is what I have been using
<?php
require "email.php";
$mail = new EMail;
$mail->Username = 'username.co.uk';
$mail->Password = 'mypassword';
$mail->SetFrom("no-reply#mywebsite.co.uk","no-reply#mywebsite.co.uk"); // Name is optional
$mail->AddTo("no-reply#mywebsite.co.uk","no-reply#mywebsite.co.uk"); // Name is optional
$mail->Subject = "Hello World Again!";
$mail->Message = "Hello World!";
//Optional
$mail->ContentType = "text/html"; // Defaults to "text/plain; charset=iso-8859-1"
$mail->Headers['X-SomeHeader'] = 'abcde'; // Set some extra headers if required
$mail->ConnectTimeout = 30; // Socket connect timeout (sec)
$mail->ResponseTimeout = 8; // CMD response timeout (sec)
$success = $mail->Send();
?>
When I test this an email gets sent but of course only the "Hello World" text gets sent as I'm unsure where to put the necessary code to pull the info I need.
Prior to moving hosts and not needing SMTP I was using this script
<?php
//-----------------------------------------------------
//-----------------------------------------------------
$address= "myemail.co.uk";
//-----------------------------------------------------
//-----------------------------------------------------
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$phone = $_REQUEST["phone"];
$message_content = $_REQUEST["message"];
$mime_boundary = md5(time());
$headers = "From: $name <$email>\n";
$headers .= "Reply-To: $subject <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$message = "-- $mime_boundary\n\n";
$message .= "New email from the Commons website: \n\n\n";
$message .= "Name: $name \n\n";
$message .= "Email: $email \n\n";
$message .= "Phone: $phone \n\n";
$message .= "Message: $message_content \n\n";
$message .= "--$mime_boundary--\n\n";
$mail_sent = mail($address, $subject, $message, $headers);
echo $mail_sent ? "Success, mail sent!" : "Mail failed";
?>
Thanks in advance
Kind Regards
Tim

Did u try as this way???
$message_str = str_replace("[first_name]", $fname, $message_str);
$message_str = str_replace("[last_name]", $lname, $message_str);
$message_str = str_replace("[site_name]", "RMS", $message_str);
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <norply#rms.com>' . "\r\n";
$subject = "activate account!";
if (mail("asd#sad.com", $subject, $message_str, $headers)) {
echo"Email successfully sent!";
} else {
echo"Email delivery failed…";
}

Just USe Php Mail function
consider below code
$subject = 'Welcome To demo.com';
$mydata['name'] = 'demo';
$mydata['user_name'] = 'test1';
$mydata['email'] = test#gmail.com;
$mydata['password'] = $password;
$mydata = (object)$mydata;
$message = "<div style='height: 500px; background: none repeat scroll 0% 0% rgb(241, 241, 241); border: 1px solid rgb(6, 106, 117); width: 450px;'>
<div style='width: 100%; background: none repeat scroll 0% 0% rgb(6, 106, 117); height: 80px;'>
<div style='width:150px; height:100px;margin:8px; float:left'>
</div>
</div>
<div style='width:100%;height:360px; margin: auto; float:left; text-align: center;'>
<h2>Hello $mydata->name </h2>
<h3>Here is you login details for Demo</h3>
<br>
<h3>Username: $mydata->user_name</h3>
<h3>Password: $mydata->password</h3>
</div>
<div style='height:30px;padding-top:10px;float:left; width:100%;text-align:center;background:#1d5770;'>
<span style='color:#fff;font-weight:bold'>demo Team</span>
</div>
</div>";
//$message = "<br><br>Username:$mydata->user_name <br><br><br>Password:$mydata->password <br>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "To: $mydata->name <$mydata->email>" . "\r\n";
$headers .= "From: demo.com <contact#demo.com>" . "\r\n";
mail($mydata->email, $subject, $message, $headers);
for further reference follow these links
http://www.w3schools.com/php/php_ref_mail.asp
http://php.net/manual/en/function.mail.php

I used this however the message I get is that the email failed
<?php
require "email.php";
$mail = new EMail;
$mail->Username = 'myemail.co.uk';
$mail->Password = 'password';
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$phone = $_REQUEST["phone"];
$message_content = $_REQUEST["message"];
$mime_boundary = md5(time());
$headers = "From: $name <$email>\n";
$headers .= "Reply-To: $subject <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$message = "-- $mime_boundary\n\n";
$message .= "New email from the Commons website: \n\n\n";
$message .= "Name: $name \n\n";
$message .= "Email: $email \n\n";
$message .= "Phone: $phone \n\n";
$message .= "Message: $message_content \n\n";
$message .= "--$mime_boundary--\n\n";
echo $mail_sent ? "Success, mail sent!" : "Mail failed";
?>

Related

How to Attach some Files In Mail in Simple PHP

I have made mail function in PHP, I use mail_to function and simple mail has been sent but the attachment file didn't. in this code I want the user send any type of file in the mail If I use any code of attachment get the error in page and page not open anyone helps me?
if(isset($_POST['submit']))
{
$pack = $_POST['pack'];
$position = $_POST['position'];
$name = $_POST['name'];
$email = $_POST['email'];
$re_no = $_POST['re_no'];
$phone = $_POST['phone'];
$rfc_no = $_POST['rfc_no'];
$comment1 = $_POST['comment1'];
$comment2 = $_POST['comment2'];
$comment3 = $_POST['comment3'];
$tandc = $_POST['tandc'];
$tandc2 = $_POST['tandc2'];
$file1 = $_POST['file1'];
$file2 = $_POST['file2'];
$file3 = $_POST['file3'];
$to = 'bhupendra.w3ondemand#gmail.com';
$subject = 'MUV Express Delivery Enqury';
$headers = "From: $email";
$message = "Package: $tp\r\n";
$message = "Package2: $pack\r\n";
$message = "Name: $name\r\n";
$message .= "Email: $email \r\n";
$message .= "Phone: $phone \r\n";
$message .= "Re No: $re_no \r\n";
$message .= "Position: $position \r\n";
$message .= "R.F.C. No.: $rfc_no \r\n";
$message .= "Comment1: $comment1 \r\n";
$message .= "comment2: $comment2 \r\n";
$message .= "comment3: $comment3 \r\n";
$message .= "Check: $tandc \r\n";
$message .= "Check2: $tandc2 \r\n";
$message .= "File1: $file1 \r\n";
$message .= "File2: $file2 \r\n";
$message .= "File3: $file3 \r\n";
$message .= "Brief Description: $briefdesc \r\n";
$sent = mail($to, $subject, $message, $headers);
$_SESSION["po"] = $_POST['position'];
if($sent) {
echo "Success";
header("Location: http://dev.w3ondemand.com/development/muvexpress/profile.php");
}
else {
echo "Failure";
header("Location: http://dev.w3ondemand.com/development/muvexpress/negocios.php");
}
}
Please map your variables accordingly.
<?php
$subject = 'Your message subject';
$encoded_content = chunk_split(base64_encode($your_file_content_here));
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$your_from_email."\r\n";
$headers .= "Reply-To: ".$reply_to_email."\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
//plain text
$message .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
//attachment
$message .="Content-Type: $file_type; name=".$file_name."\r\n";
$message .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$message .="Content-Transfer-Encoding: base64\r\n";
$message .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
$message .= $encoded_content;
$sentMailResult = mail($to, $subject, $message, $headers);
?>

send html template in php

My Php mail code is like this i want to send html content in mail but i can not send html template with this code
$to = $email;;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
// Sending email
if(mail($to, $subject, $message))
{
echo 'Your mail has been sent successfully.';
}
else
{
echo 'Unable to send email. Please try again.';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers))
{
echo 'Your mail has been sent successfully.';
} else
{
echo 'Unable to send email. Please try again.';
}
}
This kind of output i get in mail i do not know where is my mistake in this code..
Output
<html><body>
<table border="1" align="center" width="50%">
<tr><td>Test Mail</td></tr>
</table>
</body></html>
There are some errors in your code and also wrong if statement I would like to recommend following script to you for sending email.
$to = $email;
$subject = 'New Order Detail';
$message_text = 'Hi Jane';
$from = 'info#abc.co.in';
$from_name = 'John';
$to_name = 'Smith';
// Sending email
if(!empty($to) && !empty($message_text ))
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' . ucwords($to_name) .' <'.$to.'>' . "\r\n";
$headers .= 'From: ' . ucwords($from_name) . ' <'. $from. '>' ."\r\n";
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
$message .= '<p style="color:#080;font-size:18px;">'.$message_text .'</p>';
$message .= '</table>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
echo 'Your mail has been sent successfully.';
}else{
echo 'Unable to send email. Please try again.';
}
add proper header contents.
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$message .= '</body></html>';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: abc <abc#example.com>, xyz <xyz#example.com>';
$headers[] = 'From: ABC <Abc#example.com>';
$headers[] = 'Cc: abc#example.com';
$headers[] = 'Bcc: xyz#example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
Set your character set to UTF-8:
$headers .= "Content-type:text/ html;charset=UTF-8" . "\r\n";
That should fix it.
A variation on a theme perhaps and redundant as you have an answer. You should be able to add styles within the style tag region of the below code - or you could potentially add a link to an external css file but that could cause issues.
$to = $email;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
$obj=new StdClass;
$obj->success='Your mail has been sent successfully.';
$obj->fail='Unable to send email. Please try again.';
$obj->nl="\r\n";
$status = #mail( $to, $subject, $message );
if( $status ) {
exit( $obj->success );
} else {
$headers = array();
$html = array();
$headers[]="MIME-Version: 1.0";
$headers[]="From: {$from}";
$headers[]="Reply-To: {$from}";
$headers[]="X-Mailer: PHP/". phpversion();
$headers[]="MIME-Version: 1.0";
$headers[]="Content-type: text/html; charset=iso-8859-1";
$html[]="";
$html[]="<html>";
$html[]="<head>";
$html[]="<title>{$subject}</title>";
$html[]="<style>";
$html[]="p{color:red;font-size:18px;}";
$html[]="table{border:1px solid black;width:50%;display:table;}";
$html[]="td{text-align:center;background:yellow;color:blue;}";
$html[]="</style>";
$html[]="</head>";
$html[]="<body>";
$html[]="<table>";
$html[]="<tr><td>Test Mail</td></tr>";
$html[]="<tr><td><p>Will you marry me?</p></td></tr>";
$html[]="</table>";
$html[]="</body>";
$html[]="</html>";
$status = #mail( $to, $subject, implode( $obj->nl, $html ), implode( $obj->nl, $headers ) );
exit( $status ? $obj->success : $obj->fail );
}

Php contact form keeps going to spam and has an html attachement

I experiencing a problem with a php contact form that I normal use on our products sites.
<?php
$to = "martin#puritech.co.za";
$subject = "Reverse Osmosis";
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['tel'];
$mail = $_POST['message'];
$headers = "From: $name \r\n";
$headers .= "Email: $email \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html charset=iso-8859-1 \r\n";
$message = "<html><body style='font-family: Arial;'>";
$message .= "<h1>Hello Guys! You have a request from ".$name."</h1>";
$message .= "<p>".$mail."</p>";
$message .= "<p><span style='font-weight:bold;'>From:</span> ".$name."</p>";
$message .= "<span style='font-weight:bold;'>Email:</span> ".$email."</p>";
$message .= "<span style='font-weight:bold;'>Contact Number:</span> ".$number."</p>";
$message .= "</body></html>";
$send = mail ($to, $subject, $message, $headers);
if ($send==true) {
header('Location: ../index.html');
exit;
}else {?>
<script type="text/javascript">
alert('Please check your details and resend. Thank you.');
window.location = '../index.html';
</script>
<?php
}
?>
When I click on the send button, the mail goes straight to spam and has an html doc attachment.
How do I fix this? really draining me and its starting to annoy me.

PHP - why my email send code doesnt work? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
you can see on the title.
i will post here my code.. i please if i done the code worng please
<?php
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$mobile = $_REQUEST["mobile"];
//$msg = $_REQUEST["msg"];
$to = "email#email.com"; // <--- Change email ID here
if (isset($email) && isset($name)) {
$subject = "Cadastro para Pos Graduacao em Lideranca e Gestao Sustentavel";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "From: Email: $email <br/>Mobile: $mobile" ; //<br/>Message: $msg
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
well.. this codes works on other projets.. so i dont know what im doing wrong..
if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])) {
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$mobile = $_REQUEST["mobile"];
$headers = "From: information <".$email.">\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$to='email#email.com';
$subject = 'subjenct';
$message = "<html>
<body>
<p style='font-family:verdana; font-size:12px; margin:30px 0 0 30px; align=center'>
<strong> Information </strong> <br>
<strong>Full Name: </strong> ".$name."<br>
<strong>Email: </strong> ".$email."<br>
<strong>Mobile: </strong> ".$mobile."<br>
</p>
</body>
</html>";
$res= mail( $to, $subject, $message, $headers );
}
if($res)
{
echo 'success';
}
else
{
echo 'failed';
}

send email in php with attachment

i have use this code in php mail send with attachment. I am using the Apache http server with wamp.
<?php $to = 'santosh_091020#rediffmail.com';
// Declaring the necessary variables for mail sending :
$subject = 'Testing sendmail.exe';
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$mob = $_REQUEST['mob'] ;
$msg = $_REQUEST['msg'] ;
$message = ' santosh enquire \n\nName: '.$name.' \n Email:'. $email.' \n Mobile:'. $mob.' \n Message: '.$msg.' \n ';
// Declaration of the attributes for attachment.
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
fclose($fp);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Defining the contents of the header.
$headers = "From: ".$email. "\r\n" . "CC: santosh#creativecrows.com" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
//$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
//$data = chunk_split(base64_encode($data));
// sending the mail.
if(mail($to, $subject, $message, $headers))
{
echo "Email sent";
send();
}
else
{
echo "Email sending failed";
}
//send mail in client
function send()
{
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$to= trim($email);
$subject = 'thanx';
$message = '<html><head><title>HTML email</title></head>
<body style="background-color:#000099;"><p style="color:#000099;">This email contains HTML Tags!</p><table><tr><th>Firstname</th><th>Lastname</th></tr><tr><td>John</td><td>Doe</td></tr></table></body></html>';$headers1 = 'MIME-Version: 1.0' . "\r\n";
$headers1.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers1.= "From: ".$email;
if(mail($to, $subject, $message,$headers1))
{
echo "Email sent";
}
else
{
echo "Email sending failed";
echo $to;
echo $subject;
echo $message;
}
}
?>
but i got the following error;
Delivery to the following recipient failed permanently:
1.0#localhost
Technical details of permanent failure:
DNS Error: Domain name not found.
please help me. Thnx in advance.
Check this URL,
seems your header info is not absolute
http://webcheatsheet.com/php/send_email_text_html_attachment.php
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup server
$mail->Port = '';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = '';
$mail->FromName = '';
$mail->AddAddress($row['client_email'], ''); // Add a recipient
$mail->AddReplyTo('', 'Information');
$mail->AddCC('cc#example.com');
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50;
// Set word wrap to 50 characters
$mail->AddAttachment('kurtacompany/techreporting/upload/"'.$row['file1'].'"'); // Add attachments
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = '';
$mail->Body = '<p>type whatever you want</p>';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';

Categories