PHP를 통해 이메일로 HTML 전송
PHP를 사용하여 HTML 형식의 이메일을 사진과 함께 보내려면 어떻게 해야 하나요?
몇 가지 설정과 HTML 출력이 있는 페이지를 이메일로 주소로 보내 주셨으면 합니다.어떻게 해야 하나?
주된 문제는 파일을 첨부하는 것입니다.내가 어떻게 그럴 수 있을까?
그것은 꽤 간단하다.이미지를 서버에 남겨두고 PHP + CSS를 전송합니다.
$to = 'bob@example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
이 행은 메일러와 수신자에게 이메일에 올바른 형식의 HTML이 포함되어 있는 것을 통지합니다.이 행은, 해석할 필요가 있습니다.
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
여기 제가 정보를 얻은 링크가 있습니다.(링크)
보안은 필요하겠지만...
이미지의 절대 경로를 사용하여 HTML 콘텐츠를 코드화해야 합니다.절대 경로란 이미지를 서버에 업로드하고src다음과 같이 직접 경로를 지정해야 하는 이미지의 속성<img src="http://yourdomain.com/images/example.jpg">.
아래는 참조용 PHP 코드입니다.메일에서 가져온 것입니다.
<?php
// Multiple recipients
$to = 'aidan@example.com' . ', '; // Note the comma
$to .= 'wez@example.com';
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<p>Here are the birthdays upcoming in August!</p>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\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";
// Mail it
mail($to, $subject, $message, $headers);
?>
이 코드를 가지고 있기 때문에, 제 사이트에서 정상적으로 동작합니다.
public function forgotpassword($pass, $name, $to)
{
$body = "<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:support@pms.com" target="_blank">support@pms.com</a></td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body, $to, $subject);
}
메일 기능
function sendmail($body, $to, $subject)
{
//require_once 'init.php';
$from = 'testing@gmail.com';
$headersfrom = '';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: ' . $from . ' ' . "\r\n";
mail($to, $subject, $body, $headersfrom);
}
이미지 URL 기능은 이미지를 변경하는 경우에 사용합니다.한 가지 기능만 바꾸면 됩니다.패스워드를 잊어버리거나 사용자를 작성하는 등의 메일 기능이 많이 있습니다.그래서 이미지 URL 기능을 사용하고 있습니다.경로를 직접 설정할 수 있습니다.
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
HTML 이메일을 보내는 것은 일반 이메일을 PHP를 사용하여 보내는 것과 크게 다르지 않습니다.추가할 필요가 있는 것은 PHP mail() 함수의 헤더 파라미터에 따른 콘텐츠타입입니다.여기 예가 있습니다.
<?php
$to = "toEmail@domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</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\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>
W3Schools의 자세한 설명은 이쪽에서도 확인할 수 있습니다.
PHP를 통해 HTML 콘텐츠가 포함된 이메일을 쉽게 보낼 수 있습니다.다음 스크립트를 사용합니다.
<?php
$to = 'user@example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: CodexWorld<info@codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome@example.com' . "\r\n";
$headers .= 'Bcc: welcome2@example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
소스 코드와 라이브 데모는 여기에서 확인할 수 있습니다 - PHP를 사용하여 아름다운 HTML 이메일 보내기
가장 간단한 방법은 Zend Framework 또는 Cake와 같은 다른 프레임워크를 사용하는 것입니다.PHP 또는 Symfony.
표준으로 할 수 있습니다.mail기능하기도 하지만 사진을 첨부하는 방법에 대한 지식이 조금 더 필요합니다.
또는 이미지를 첨부하는 대신 서버에서 호스트합니다.HTML 메일의 송신에 대해서는, mail() 함수의 메뉴얼에 기재되어 있습니다.
PHPMailer를 사용합니다.
HTML 메일을 송신하려면 , $mail-> is 를 설정할 필요가 있습니다.HTML()에만 해당되며 HTML 태그를 사용하여 본문을 설정할 수 있습니다.
다음은 잘 작성된 튜토리얼입니다.
HTML 본문 부분을 작성할 때 이미지 MIME 부분의 내용 ID를 아는 것이 중요합니다.
요약하면 img 태그를 만드는 것입니다.< img src = " cid : enter contentidhere " / >
buildMimeMessage 함수의 동작 예를 참조하십시오.
언급URL : https://stackoverflow.com/questions/11238953/send-html-in-email-via-php
'programing' 카테고리의 다른 글
| mariadb 서버 로컬 설치로 mariadb의 도커를 실행할 수 있습니까? (0) | 2022.10.22 |
|---|---|
| 애플리케이션 스레드를 안전하게 만드는 방법 (0) | 2022.10.22 |
| stdClass 개체를 다른 클래스로 변환/캐스팅합니다. (0) | 2022.10.22 |
| 많은 인수를 메서드에 전달하기 위한 베스트 프랙티스? (0) | 2022.10.22 |
| MySQL Workbench - 테이블 데이터 가져오기 마법사(모든 날짜/시간 개체를 0000-00 00:00:00:00로 기록) (0) | 2022.10.22 |