How to make PHP Mail() function use a specific SMTP server? - php

How do you make php's mail function send email from a specific separate SMTP server instead of using the local host?
My code:
<?php
$to = "user#AOL.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>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>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply#foo.com>' . "\r\n";
mail($to,$subject,$message,$headers);
?>
Background info:
I currently have a Linux server running Apache and also acting as a mail server (foo.com) and a separate server (bar.com) running a LAMP stack.
On bar.com I am trying to code a auto mailer for users registering accounts that sends the emails from foo.com.

mail() does not support smtp. You need to use a mail framework which supports smtp like: http://pear.php.net/package/Mail
See: smtp configuration for php mail

Related

PHP : How to use dynamic variables

I'm trying to send HTML email from my Custom Controller.
Here's code.
public function execute()
{
$to = "dev#test.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>'.$_POST["company_name"].'</td>
</tr>
</table>
</body>
</html>
";
// 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: <webmaster#example.com>';
mail($to,$subject,$message,$headers);
How Can I use dynamic variables based on POST request so that Email Send perfectly with Dynamic form.
I've tried as I paste above code but still getting syntax erros in PHP Strom.
Any Help would be appreciated.
Instead of:
<td>'.$_POST["company_name"].'</td>
use:
<td>".$_POST['company_name']."</td>
Your HTML is quoted with double quotation so to return to PHP you also need to use double one.
Two things. For multiline strings, I'd recommend using heredoc. Secondly, save the form data into a variable before using it in the string.
$company_name = $_POST['company_name'];
$message = <<<EOD
<html>
<h1>$company_name</h1>
<html>
EOD;

Unable to send mail using php mail() function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
http://webprojects.co/dev_wed/mail.php trying to send mail from server using this script but i am not been able to send mail from server
<?php
$to = "test#gmail.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>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>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webprojects#webprojects.com>' . "\r\n";
$headers .= 'Cc: webprojects#webprojects.com' . "\r\n";
$result = mail($to,$subject,$message,$headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
?>
always display error trying to send email using php mail() function
Are you testing the code online or via localhost? The mail() function doesn't work in localhost without SMTP.

Customers email displays HTML tags on PHP system generated mail

When I send a really basic HTML email via a file using PHP mail it displays perfectly for me. But for my client he sees all the HTML tags in the email.
I have my own dedicated server, to try to problemsolve I ran the exact same file from a Heart Internet shared server I have and the email displayed perfectly to my client (no HTML tags) so it points to something to do with the server.
<?php
$to = 'tom#myemailaddress.com';
$subject = 'TOM Testing: HTML displaying correctly?';
$message = '
<html>
<head>
<title>This is a test</title>
</head>
<body>
<p>To see if</p>
<table>
<tr>
<th>You</th><th>See</th><th>The</th><th>HTML</th>
</tr>
<tr>
<td>or</td><td>it</td><td>is</td><td>formatted</td>
</tr>
<tr>
<td>correctrly</td><td>1</td><td>2</td><td>abc</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";
// Mail it
mail($to, $subject, $message, $headers);
?>
Here is my php info: http://www.saloponline.co.uk/phpinfo.php

How to know my mail send to recevier in php

I have some problem in sending the mail, my mail not send to the receiver.. but get back email to me. my code is here please suggest me
<?php
if(isset($_POST["c_submit"]))
{
if(($_POST['captcha']!="") || ($_SESSION['captcha_id'])!="")
{
if($_POST['captcha']==$_SESSION['captcha_id']) {
$to_customer = $_POST["mail"];
$subject = "Thanking you for Contacting US";
$mail_body = "
<html>
<head>
<title>Thanking you for Contacting</title>
</head>
<body>
<font face='Verdana'>
Dear ".$_POST["name"].",</br>
<p>Thank you for visiting our website. We have received your enquiry through our web form. We appreciate you considering</p>
<p>Sincerely,</p>
<b>Penis Plug. Ltd.</b><br />
Website:". $site."<br />
Email:" .$email."<br />
<br />
<br />
</font>
</body>
</html>";
$to = "ashish.sws#gmail.com";
$mail_subject = "Contact Form Mail";
$to_mail_body = "
<htm>
<head>
<title>Customer Details</title>
</head>
<body>
<font face='Verdana'>
<table>
<tr>
<th>Name : </th>
<td>".$_POST["name"]."</td>
</tr>
<tr>
<tr>
<th>Message : </th>
<td>".$_POST["message"]."</td>
</tr>
<tr>
<th>Email </th>
<td>".$_POST["email"]."</td>
</tr>
<tr>
<th>Contact No. : </th>
<td>".$_POST["phone"]."</td>
</tr>
<tr>
<th>Company : </th>
<td>".$_POST["company"]."</td>
</tr>
</table>
</font>
</body>
</html>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.'http://www.swatiwebtechnologies.com'.'<'.'ashish.sws#gmail.com'.'> ' . "\r\n";
if(#mail($to_customer,$subject,$mail_body,$headers))
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
#mail($to,$mail_subject,$to_mail_body,$headers);
} else if($_POST['captcha']!=""){
echo '<font color="red"><b>Not Matching, Try Again...</b></font>';
}
}
}
?>
Here i am sending two mail 1 for customer and one for self in customer mail problem arise and self email working fine..plese suggest me
As per the docs, the function returns false on failure to send.
$flgSend = #mail(...);
If this causes your code to show "Mail send completed.", but it still doesn't appear in your inbox, then either it just hasn't arrived yet or a SPAM filter has caught it.
Try this for checking of email sending
if(#mail($to_customer,$subject,$mail_body,$headers))
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
Suggestions:
What is the exact error, if you remove the error suppression operator?
Are you sure the http://www.com in the from: part is valid?
Many spam blocking list providers will block your mail if you don't authenticate encryped. Then, an answer get back to your inbox that your mail was not delivered.
Search here for "mail php", it will bring you dozens of ideas. Like this:
PHP mail() - mail not sending

how to send image mailer using php mail function

i am trying to send html image mailer using php mail function on linux platform. There is one issue when i am trying to send simple html content then it successfully get delivered to its subscriber. but when i try to send emailer which contains few images then it fails to deliver it. below are two codes 1 by which simple html is delivered. 2 which is not getting delivered.
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>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>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
code 2:
<?php
//change this to your email.
$to = "abhinav#xyz.in";
$from = "abhinav.1#gmail.com";
$subject = "Hello! This is HTML email";
//begin of HTML message
$message= <<<EOF
<html>
<body bgcolor="#DCEEFC">
<center>
<b>Looool!!! I am reciving HTML email......</b> <br>
<font color="red">Thanks Mohammed!</font> <br>
* maaking.com
</center>
<br><br>*** Now you Can send HTML Email <br> Regards<br>MOhammed Ahmed - Palestine
</body>
</html>
EOF;
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
//options to send to cc+bcc
//$headers .= "Cc: [email]maa#p-i-s.cXom[/email]";
//$headers .= "Bcc: [email]email#maaking.cXom[/email]";
// now lets send the email.
mail($to, $subject, $message, $headers);
echo "Message has been sent....!";
?>
I corrected the code by which I am able to send image mailer below is the code:
<?php
$to= 'abhinav#xyz.com' . ',';
$to .= 'abhinav#gmail.com';
$sub='test1';
$msg= <<<EOF
<html>
<body>
<table>
<tr>
<td><img src="http://d32vlg867bsa1v.cloudfront.net/z/prod/w/2/i/zovi-logo2.png" />
</td>
</tr>
</table>
</body>
</html>
EOF;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: <centos_test#example.com>' . "\r\n";
mail($to,$sub,$msg,$headers);
?>
I am still working to sent highly customized mailer and will describe how it can once I found how it can be done.

Categories